HTML and CSS Reference
In-Depth Information
<form action=”postmultiplevalues.php” method=”post”>
<input type=”checkbox” name=”colors[]” value=”red” />
Red
<br />
<input type=”checkbox” name=”colors[]” value=”green” />
Green
<br />
<input type=”checkbox” name=”colors[]” value=”blue” />
Blue
</form>
When the form is submitted, you can access the values as you would for any other para-
meter, except that the value in the
$_REQUEST
array for this parameter will be an array
rather than a single value. You can access it like this:
$colors = $_REQUEST['colors'];
foreach ($colors as $color) {
echo “$color<br />\n”;
}
If the user selects only one check box, the value will be placed in an array that has only
one element.
▼
Task:
Exercise 21.1: Validating a Form
One of the most common tasks when it comes to server-side processing is form valida-
tion. When users submit data via a form, it should be validated on the server, even if
your page includes JavaScript validation, because you can't guarantee that JavaScript
validation was actually applied to the form data.
I use a simplified version of the user registration form from Lesson 11 in this exercise.
Figure 21.1 is a screenshot of the form I'll be using. Here's the HTML source:
Input
▼
<!DOCTYPE html>
<html>
<head>
<title>
Registration Form
</title>
</head>
<body>
<h1>
Registration Form
</h1>
<p>
Please fill out the form below to register for our site. Fields
with bold labels are required.
</p>
<form method=”post”>
<p><label for=”name”><b>
Name:
</b></label><br />
<input name=”name” /></p>