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. If we name the SELECT element with a plain name, like so

 <select name="products" multiple> 

the script that receives this data has access to only a single value corresponding to this name. We can change this behavior by renaming an element of this kind so that its name ends with an empty set of square brackets. We do this in Listing 9.4.

Listing 9.4 An HTML Form Including a SELECT Element
   1: <html>   2: <head>   3: <title>Listing 9.4 An HTML form including a SELECT element</title>   4: </head>   5: <body>   6: <form action="listing9.5.php" method="POST">   7: Name: <br>   8: <input type="text" name="user">   9: <br>  10: Address: <br>  11: <textarea name="address" rows="5" cols="40"></textarea>  12: <br>  13: Pick Products: <br>  14: <select name="products[]" multiple>  15: <option>Sonic Screwdriver</option>  16: <option>Tricorder</option>  17: <option>ORAC AI</option>  18: <option>HAL 2000</option>  19: </select>  20: <br><br>  21: <input type="submit" value="hit it!">  22: </form>  23: </body>  24: </html> 

Put these lines into a text file called listing9.4.php, and place that file in your Web server document root. Next, in the script that processes the form input, we find that input from the "products[]" form element created on line 14 is available in an array called $_POST[products]. Because products[] is a SELECT element, we offer the user multiple choices using the option elements on lines 15 through 18. We demonstrate that the user's choices are made available in an array in Listing 9.5.

Listing 9.5 Reading Input from the Form in Listing 9.4
   1: <html>   2: <head>   3: <title>Listing 9.5 Reading input from the form in Listing 9.4</title>   4: </head>   5: <body>   6: <?php   7: print "Welcome <b>$_POST[user]</b><p>\n\n";   8: print "Your address is:<p>\n\n<b>$_POST[address]</b><p>\n\n";   9: print "Your product choices are:<p>\n\n";  10: if (!empty($_POST[products])) {  11:     print "<ul>\n\n";  12:     foreach ($_POST[products] as $value) {  13:        print "<li>$value\n";  14:        }  15:        print "</ul>";  16: }  17: ?>  18: </body>  19: </html> 

Put these lines into a text file called listing9.5.php, and place that file in your Web server document root. Now access the form in Listing 9.4 with your Web browser and fill out the fields. Figure 9.3 shows an example.

Figure 9.3. Form created in Listing 9.4.


On line 7 of the script in Listing 9.5, we access the $_POST[user] variable, which is derived from the user form element. On line 10, we test for the $_POST[products] variable. If $_POST[products] is present, we loop through it on line 12, and output each choice to the browser on line 13.

Submit the form and you might see something like that shown in Figure 9.4.

Figure 9.4. Sample output of Listing 9.5.


Although the looping technique is particularly useful with the SELECT element, it works with every form element. For example, by giving a number of check boxes the same name, you can enable 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 elements from lines 15-18 in Listing 9.4 with a series of check boxes to achieve 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> 

Sams Teach Yourself PHP, MySQL and Apache in 24 Hours
Sams Teach Yourself PHP, MySQL and Apache in 24 Hours
ISBN: 067232489X
EAN: 2147483647
Year: 2005
Pages: 263 © 2008-2017.
If you may any questions please contact us: