Webocreation

Monday, January 4, 2010

Accessing Form Input with User-Defined Arrays

Accessing Form Input with User-Defined Arrays

The examples so far enable us to gather information from HTML elements that submit a single value per element name. This leaves us with a problem when working with select elements. These elements make it possible for the user to choose multiple items. Suppose we name the select element with a plain name:
<select name="products" multiple="multiple">  
The script that receives this data will only have access to a   single value corresponding to this name in $_REQUEST['products']. We   can change this behavior by renaming any elements of this kind so that its name   ends with an empty set of square brackets. We do so in Listing 10.4.
Listing 10.4 An HTML Form with a select Element
1: <!DOCTYPE html PUBLIC   2:     "-//W3C//DTD XHTML 1.0 Strict//EN"   3:     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">   4: <html>   5: <head>   6: <title>Listing 10.4 An HTML Form with a 'select' Element</title>   7: </head>   8: <body>   9: <div>  10: <form action="listing10.5.php" method="post">  11: <p><input type="text" name="user" /></p>  12: <p>  13: <textarea name="address" rows="5" cols="40">  14: </textarea>  15: </p>  16: <p>  17: <select name="products[]" multiple="multiple">  18: <option>Sonic Screwdriver</option>  19: <option>Tricorder</option>  20: <option>ORAC AI</option>  21: <option>HAL 2000</option>  22: </select>  23: </p>  24: <p><input type="submit" value="hit it!" /></p>  25: </form>  26: </div>  27: </body>  28: </html>  
In the script that processes the form input, we now find that input from the "products[]" form element created on line 17 will be available as an array indexed by the name products in either $_POST or $_REQUEST. products[] is a select element, and we offer the user multiple choices using the option elements on lines 18 to 21. We demonstrate that the user's choices are made available in an array in Listing 10.5.
Listing 10.5 Reading Input from the Form in Listing 10.4
1: <!DOCTYPE html PUBLIC   2:     "-//W3C//DTD XHTML 1.0 Strict//EN"   3:     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">   4: <html>   5: <head>   6: <title>Listing 10.5 Reading Input from the Form in Listing 10.4</title>   7: </head>   8: <body>   9: <div>  10: <?php  11: print "Welcome <b>".$_POST['user']."</b><br/>\n";  12: print "Your address is:<br/><b>".$_POST['address']."</b><br/>\n";  13:  14: if ( is_array( $_POST['products'] ) ) {  15: print "<p>Your product choices are:</p>\n";  16: print "<ul>\n";  17:     foreach ( $_POST['products'] as $value ) {  18:        print "<li>$value</li>\n";  19:     }  20: print "</ul>\n";  21: }  22: ?>  23: </div>  24: </body>  25: </html>  
On line 11, we access the $_POST['user'] element, which is derived from the user form element. On line 14, we test the $_POST['products'] element. If the element is an array as we expect, we loop through it on line 17, outputting each choice to the browser on line 18.
Although this technique is particularly useful with the select element, in fact it works with any form element at all. By giving a number of check boxes the same name, for example, you can allow a user to choose many values within a single field name. As long as the name you choose ends with empty square brackets, PHP compiles the user input for this field into an array. We can replace the select element from lines 12–17 in Listing 10.4 with a series of check boxes to achieve exactly the same effect:
<input type="checkbox" name="products[]" value="Sonic Screwdriver" />Sonic Screwdriver<br/>  <input type="checkbox" name="products[]" value="Tricorder" />Tricorder<br/>  <input type="checkbox" name="products[]" value="ORAC AI" />ORAC AI<br/>  <input type="checkbox" name="products[]" value="HAL 2000" />HAL 2000<br/>  
s

No comments:

Post a Comment