wright67uk Posted January 20, 2013 Share Posted January 20, 2013 (edited) I've taken this as an extract of a much larger file. If $score1 is empty the no score message appears, the exit() occurs. and the footer div will not appear. If $score1 is filled, the success message and the footer div both appear fine. If I remove the exit, then both the success and the no score message appears, and the footer div displays fine. I would like the no score message to appear when $score1 is empty, aswell as the footer div to appear. How should I restructure my code so that is so? <input type="text" name="score"/> <?php if(isset($_POST['processForm'])) { $score1 = $_POST['score']; if (empty($score1)) {echo "You haven't entered a score for score"; exit(); } #connection here $sql = "INSERT STATEMENT HERE"; mysql_query($sql); echo "success message"; }; ?> </form> <div id="footer>... Edited January 20, 2013 by wright67uk Quote Link to comment https://forums.phpfreaks.com/topic/273391-when-empty-prevent-insert-but-display-a-message/ Share on other sites More sharing options...
PFMaBiSmAd Posted January 20, 2013 Share Posted January 20, 2013 The method that we generally recommend is to use an array to hold your application error messages. You would add each error message as an element to the array. You can then test at any point if the array is empty or not to decide if you want to do anything. After you have performed all your validation tests, if the array is not empty, you would loop over the entries in it and output the error messages. if it is empty, you would form and run your query and if the query is successful and actually inserts a row, you would output your success message. Quote Link to comment https://forums.phpfreaks.com/topic/273391-when-empty-prevent-insert-but-display-a-message/#findComment-1407090 Share on other sites More sharing options...
wright67uk Posted January 20, 2013 Author Share Posted January 20, 2013 Thankyou for the reply. And out of interest what would be the method if there were only 2 or even just 1 input? Quote Link to comment https://forums.phpfreaks.com/topic/273391-when-empty-prevent-insert-but-display-a-message/#findComment-1407107 Share on other sites More sharing options...
Psycho Posted January 20, 2013 Share Posted January 20, 2013 Thankyou for the reply. And out of interest what would be the method if there were only 2 or even just 1 input? Create a standard process for doing your form validations and error handling - then just use it. You *could* modify the logic if you only have one or two fields, but why? Quote Link to comment https://forums.phpfreaks.com/topic/273391-when-empty-prevent-insert-but-display-a-message/#findComment-1407112 Share on other sites More sharing options...
wright67uk Posted January 20, 2013 Author Share Posted January 20, 2013 Well I just wasn't sure if you would make an array with just one value... Although I'm happy listening to the experts :-) Quote Link to comment https://forums.phpfreaks.com/topic/273391-when-empty-prevent-insert-but-display-a-message/#findComment-1407113 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.