168 | Chapter 7: Web Techniques Multivalued Parameters HTML selection lists, created with the
select
tag, can allow multiple selections. To
ensure that PHP recognizes the multiple values that the browser passes to a form-
processing script, you need to make the name of the field in the HTML form end
with
[]
. For example:
Now, when the user submits the form,
$_GET['languages']
contains an array instead
of a simple string. This array contains the values that were selected by the user.
Example 7-6 illustrates multiple selection. The form provides the user with a set of
personality attributes. When the user submits the form, he gets a (not very interest-
ing) description of his personality.
$fahr = $_GET['fahrenheit'];
?>
if (! is_null($fahr)) {
$celsius = ($fahr - 32) * 5/9;
printf("%.2fF is %.2fC", $fahr, $celsius);
}
?>