Jump to content

How to Display Errors on Form Submission


doubledee

Recommended Posts

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

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.