Using Arrays with Forms

Arrays are particularly potent when used in combination with form elements that support more than one value, such as multiple-selection list boxes or grouped checkboxes. To capture a user's input in an array, simply add square braces to the form element's ' name' to automatically convert it into a PHP array when the form is submitted.

The easiest way to illustrate this is with an example. Consider the following form, which holds a multiple-selection list of popular music artists:

<form method="post" action="array-form.php"> Select your favourite artists: <br /> <select name="artists[]" multiple="true">

<option value="Britney Spears">Britney Spears</option> <option value="Aerosmith">Aerosmith</option> <option value="Black-Eyed Peas">Black-Eyed Peas</option> <option value="Diana Ross">Diana Ross</option> <option value="Foo Fighters">Foo Fighters</option>

<input type="submit" name="submit" value="Submit" />

Notice the 'name' attribute of the <select> element, which is named artists []. This tells PHP that, when the form is submitted, all the selected values from the list should be converted into elements of an array. The name of the array will be $_POST [' artists ' ], and it will look something like this:

Array (

A practical application will make it easier to understand how PHP interacts with array variables and forms. This next application presents the user with a form containing various pizza toppings and asks the user to select, via checkbox, his or her favorite toppings. Here's the form (pizza.html):

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/19 9 9/xhtml" xml:lang="en" lang= <head>

<title>Project 4-2: Pizza Topping Selector</title> </head> <body>

<h2>Project 4-2: Pizza Topping Selector</h2> <form method="post" action="pizza.php">

Select your favourite pizza toppings: <br />

<input type="checkbox" name="toppings[

<input type="checkbox" name="toppings[

<input type="checkbox" name="toppings[ peppers</input>

<input type="checkbox" name="toppings[

<input type="checkbox" name="toppings[

<input type="checkbox" name="toppings[

<input type="checkbox" name="toppings[

<input type="checkbox" name="toppings[

<input type="checkbox" name="toppings[

<input type="checkbox" name="toppings[

<input type="checkbox" name="toppings[ <p>

<input type="submit" name="submit" value="Submit" /> </form> </body> </html>

value="tomato">Tomato</input>

value="onion">Onion</input>

value="jalapenos">Jalapeno value="olives">Olives</input>

value="mint">Mint</input>

value="pineapple">Pineapple</input>

value="bacon">Bacon</input>

value="chicken">Chicken</input>

value="ham">Ham</input>

value="anchovies">Anchovies</input>

value="x-cheese">Extra cheese</input>

Figure 4-2 illustrates what the form looks like.

And here's the code for the form submission script (pizza.php):

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

"DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/19 9 9/xhtml" xml:lang="en" lang="en"> <head>

<title>Project 4-2: Pizza Topping Selector</title> </head> <body>

<h2>Project 4-2: Pizza Topping Selector</h2> You selected the following toppings: <br /> <ul> <?php foreach ($_POST['toppings'] as $t) { echo "<li>$t</li> \r\n";

Figure 4-2 A Web form to select pizza toppings

When the Web form is submitted, PHP automatically places all the checked values into the $_POST [' toppings ' ] array. This array can then be processed like any other array—in this case, with a f oreach loop that prints the selected toppings in an ordered list. Figure 4-3 illustrates this output.

3 Project 4-2: Pizza Topping Selector - Mozilla Firefon

EIIPMI

File Edit View History Bookmarks Tools Help

v-- ft

i

l_| http://localhost/php6-book/ch04/project:02/pizza,php

a

m

1

Was this article helpful?

+1 0

Post a comment