doubledee Posted April 20, 2012 Share Posted April 20, 2012 I have a Q&A Form that consists of 10 Questions with input boxes for 10 Answers. (Each Answer is stored in its own record in the "answer" table.) When the Form is submitted, I loop through an answerArray and decide what to do with each Form Field. For example, if there is a new Answer, then I create a new record by doing an INSERT, but if the Answer is a change, then I do an UPDATE on an existing record. Following my previous coding style, I assign a Results Code for *every* possible thing that can happen in *every* code branch, e.g. $_SESSION['resultsCode'] = 'ANSWERS_NO_CHANGES_2138'; $_SESSION['resultsCode'] = 'ANSWERS_UPDATE_SUCCEEDED_2139'; $_SESSION['resultsCode'] = 'ANSWERS_UPDATE_FAILED_2140'; $_SESSION['resultsCode'] = 'ANSWERS_INSERT_SUCCEEDED_2141'; $_SESSION['resultsCode'] = 'ANSWERS_INSERT_FAILED_2142'; The problem is that - as my code currently stands - I only end up displaying a resultsCode for the LAST QUESTION, because the first 9 are overwritten?! Should I keep my current structure, and quit on an Errors, and display a page with that error (e.g. Update Failed), and then just comment out the Succeed messages? Or do I get fancy and store each Success or Error in an array and display the outcome for all 10 Questions after the Form is processed? In the past all of this was easy, because ONE FORM equated to ONE RECORD, so Error Messages were easier to display. But here, I have ONE FORM and up to 10 RECORDS?! Hope this is making sense? Thanks, Debbie Quote Link to comment https://forums.phpfreaks.com/topic/261289-how-to-display-errors-on-form-submission/ Share on other sites More sharing options...
creata.physics Posted April 20, 2012 Share Posted April 20, 2012 You can have an array to store errors like you currently do and only process the code needed to be executed if there aren't any errors. A basic way to handle errors can be done like so: <?php $errors = ''; // errors is just an empty string if( isset( $_POST['process'] ) ) { // You will need to of course implement all the neccessary checks to determine what you consider invalid if( $condition == $trip_error ) { $errors[] = ERROR_CONSTANT; } if( ! is_array( $errrors ) ) { // We didn't trip any errors, execute code } } if ( is_array( $errors ) ) { foreach( $errors as $error ) { $error_output .= $error; } } ?> <html> <body> <?php echo $error_output; ?> <form action="" method="post"> <!--FORM_STUFF--> </form> $errors is an empty string, so if we assign an array to it that is how you can check if any errors do exist. Try to understand the logic I'm giving you and how to implement it into your current design. Quote Link to comment https://forums.phpfreaks.com/topic/261289-how-to-display-errors-on-form-submission/#findComment-1338979 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.