Forum Moderators: coopster
if (!isset($_POST['suggestion']) ¦¦ empty($_POST['suggestion'])) $errmsg .= "I'm sorry $name it seems like you have forgotten to include your comments in the form";
I have five different error messages depending on section not filled in, in the form. Or would it be better to set five different error message names like $errmsg1 = url(errmsg1.php);
or somthing to that effect. Sorry I still dont think like a programer and this is the only way I can think of stating what I would like to try
I think that the most cohesive way to do it is to use just one script containing functions for each part of the process, like form_display(), form_validation(), and process_success(). That way you can use a series of switches to choose the appropriate function, with the default being form_display().
Once the form is submitted by a visitor, it would go to form_validation(). If it validates, you would then call process_success(). But if it doesn't validate, the form_validation() function would set your error messages and again call form_display(), passing back to it the data that the visitor had entered (so he doesn't have to renter it), plus the error messages.
In your form validation function, you can do something like:
$errors = 0;
if(!/*suggestion validation stuff*/) {
$suggestion error = "<span style=\"color:red;\">I'm sorry....</span><br>\n";
$errors++;
}
...
Then at the end of form_validation(), do something like:
if ($errors > 0) form_display();
else process_success();
On failure, a nice way to display the errors is to put them immediately above whatever form field they pertain to. For example, back on your form, immediately above the "suggestion" field, put:
echo $suggestion_error;
Of course, if $suggestion_error is empty, it will echo nothing at all, so it will never display unless $suggestion was a cause of an invalid submission. If $errors > 0, you might also want to set a more generalized error message to put at the top of the form.
Also use the values from the original submission in the form, like:
<textarea cols=40 rows=3 name="suggestion"><? echo $suggestion?></textarea>
Again, because $suggestion will be empty on a first visit to the form, no content will be displayed until the form has been submitted at least once. Of course, at the top of your form_display() function you will want to initialize all of your form variables for when it is first visited, like:
if (!isset($suggestion)) $suggestion = "";
if (!isset($suggestion_error)) $suggestion_error = "";
Overall, I think this might give you the consistency and professional appearance that you want.
I wish you well,