Prev | Current Page 419 | Next

W. Jason Gilmore

"Beginning PHP and MySQL: From Novice to Professional"

Specifically, you would like to
ask the user to indicate those programming languages that interest him. Using a few
text fields along with a multiple-select box, this form might look similar to that shown
in Figure 13-1.
Figure 13-1. Representing the same data using two different form items
The HTML for the multiple-select box shown in Figure 13-1 might look like this:

Because these components are multivalued, the form processor must be able to
recognize that there may be several values assigned to a single form variable. In the
preceding examples, note that both use the name languages to reference several
language entries. How does PHP handle the matter? Perhaps not surprisingly, by
considering it an array. To make PHP recognize that several values may be assigned
to a single form variable, you need to make a minor change to the form item name,
appending a pair of square brackets to it. Therefore, instead of languages, the name
would read languages[]. Once renamed, PHP will treat the posted variable just like any
other array. Consider a complete example in the script multiplevaluesexample.php:
CHAPTER 13 ?–  FORMS 355
if (isset($_POST['submit']))
{
echo "You like the following languages:
";
foreach($_POST['languages'] AS $language) {
$language = htmlentities($language);
echo "$language
";
}
}
?>
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431