ptg16476052
Processing Forms 675
24
If there are errors, I want to load the form and show the errors inline. This is the easiest
way to show customers exactly what mistakes they made. Before I do that, let me show
you one more thing I’ve added to the page. I included a style sheet that defines one rule:
.error. The error messages for any fields with errors will be assigned to this class so that
they can be highlighted when the form is presented. Here’s the style sheet:
Okay, now that everything is set up, let’s look at how the name field is presented. Here’s
the code:
This code is different from the old code I used to present the name field in the original
listing in this example. I included the value as an escaped version of what might have
been submitted in the $name variable with <?php echo htmlspecialchars($name);?>. I
also included a span with the $nameErr error message inside it. If there is no error, then
that tag will display nothing. This message will be red, indicating to the user that she
needs to fix that field.
The age field is identical to the name field in every way except its variable names, so I
skip that and turn instead to the toys field. Here’s the code:
$options = array('Digital Camera','MP3 Player','Tablet');
foreach ($options as $option) {
echo '
" ;
}
?>
▼
▼