Changes

Jump to: navigation, search

PHP and HTML Forms

1,404 bytes added, 20:11, 5 June 2007
Processing Form Data Using PHP
You have requested our newsletter
</tt>
 
== Processing Multiple Selections with PHP ==
 
In [[An Overview of HTML Forms]] we covered the HTML Select input and described the steps required to configure the object for multiple selection. For example the following HTML fragment creates a Select input in which multiple car types may be selected by the user:
 
<pre>
<select name="carBrands" size=5 multiple>
<option value="Ford" SELECTED>Ford Motor Company
<option value="GM">General Motors
<option value="Honda">Honda Motor Company
<ottion value="Toyota">Toyota Motor Company
<option value="Ford">Jaguar
<option value="Mazda">Mazda
<option value="Volvo">Volvo
</select>
</pre>
 
Up until now we have only concerned oursleves with data whcih can only be of one particular value. Clearly, in this case there is the potential for multiple data values being associated with a single form element. The way PHP handles this is place the multiple selections in an array. Before that happens, however, we need to make a small modification to our declaration of the HTML Select element. All we need to do to turn this data into an array is to place [] after the element name:
 
<select name="carBrands[]" size=5 multiple>
 
Now, in our server side PHP script we can extract the selected items from this array:
 
<pre>
print_r($_POST['carBrands']);
</pre>
 
Assuming we selected GM and Ford the output would read:
 
<tt>Array ( [0] => Ford [1] => GM )</tt><br>

Navigation menu