Home > Articles > Programming > PHP

  • Print
  • + Share This
This chapter is from the book

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

<select name="products" multiple>

the script that receives this data will only have access to a single value corresponding to this name. 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 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: <input type="text" name="user">
 8: <br>
 9: <textarea name="address" rows="5" cols="40">
10: </textarea>
11: <br>
12: <select name="products[]" multiple>
13: <option>Sonic Screwdriver
14: <option>Tricorder
15: <option>ORAC AI
16: <option>HAL 2000
17: </select>
18: <br>
19: <input type="submit" value="hit it!">
20: </form>
21: </body>
22: </html> 

In the script that processes the form input, we now find that input from the "products[]" form element created on line 12 will be available in an array called $products. products[] is a select element, and we offer the user multiple choices using the option elements on lines 13 to 16. 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>$user</b><p>\n\n";
 8: print "Your address is:<p>\n\n<b>$address</b><p>\n\n";
 9: print "Your product choices are:<p>\n\n";
10: if ( ! empty( $products ) ) {
11:   print "<ul>\n\n";
12:   foreach ( $products as $value ) {
13:    print "<li>$value<br>\n";
14:   }
15:   print "</ul>";
16: }
17: ?>
18: </body>
19: </html> 

On line 7 we access the $user variable, which is derived from the user form element. On line 10 we test for the $products variable. If it is present we loop through it on line 12, outputting each choice to browser on line 13.

Although this technique is particularly useful with the SELECT element, it will in fact work 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 9.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>

In fact, we are not limited to numerically indexed arrays. We can place form input data into associative arrays, and even into multidimensional arrays. To keep our script's data neat, for example, we might wish to place all form input into an associative array called $form. We can do this very simply by constructing our form field names as if they were elements in an associative array (once again, omitting the dollar sign).

<input type="text" name="form[user]"><br>
<textarea name="form[address]" rows="5" cols="40">
</textarea>

Once submitted we will then be able access 'user' and 'address' as elements in the $form array.

print $form[user];

To construct a multidimensional array, we can simply extend the associative array naming convention to include another level

<input type="checkbox" name="form[products][]" value="Sonic Screwdriver">Sonic Screwdriver<br>
<input type="checkbox" name="form[products][]" value="Tricorder">Tricorder<br>
<input type="checkbox" name="form[products][]" value="ORAC AI">ORAC AI<br>
<input type="checkbox" name="form[products][]" value="HAL 2000">HAL 2000<br>

When submitted, the $form[products] element should contain a numerically indexed array, populated according to the checkboxes clicked by the user.

  • + Share This
  • 🔖 Save To Your Account