Приглашаем посетить
Толстой (tolstoy-lit.ru)

6.2 Accessing Form Parameters

Previous Table of Contents Next

6.2 Accessing Form Parameters

At the beginning of every request, the PHP interpreter sets up some auto-global arrays that contain the values of any parameters submitted in a form or passed in the URL. URL and form parameters from GET method forms are put into $_GET. Form parameters from POST method forms are put into $_POST.

The URL http://www.example.com/catalog.php?product_id=21&category=fryingpan puts two values into $_GET: $_GET['product_id'] is set to 21 and $_GET['category'] is set to fryingpan. Submitting the form in Example 6-2 causes the same values to be put into $_POST, assuming 21 is entered in the text box and Frying Pan is selected from the menu.

Example 6-2. A two-element form
<form method="POST" action="catalog.php">

<input type="text" name="product_id">

<select name="category">

<option value="ovenmitt">Pot Holder</option>

<option value="fryingpan">Frying Pan</option>

<option value="torch">Kitchen Torch</option>

</select>

<input type="submit" name="submit">

</form>

Example 6-3 incorporates the form in Example 6-2 into a complete PHP program that prints the appropriate values from $_POST after displaying the form. Because the action attribute of the <form> tag in Example 6-3 is catalog.php, you need to save the program in a file called catalog.php on your web server. If you save it in a file with a different name, adjust the action attribute accordingly.

Example 6-3. Printing submitted form parameters
<form method="POST" action="catalog.php">

<input type="text" name="product_id">

<select name="category">

<option value="ovenmitt">Pot Holder</option>

<option value="fryingpan">Frying Pan</option>

<option value="torch">Kitchen Torch</option>

</select>

<input type="submit" name="submit">

</form>

Here are the submitted values:



product_id: <?php print $_POST['product_id']; ?>

<br/>

category: <?php print $_POST['category']; ?>

A form element that can have multiple values needs to have a name that ends in [ ]. This tells the PHP interpreter to treat the multiple values as array elements. The <select> menu in Example 6-4 has its submitted values put into $_POST['lunch'].

Example 6-4. Multiple-valued form elements
<form method="POST" action="eat.php">

<select name="lunch[  ]" multiple>

<option value="pork">BBQ Pork Bun</option>

<option value="chicken">Chicken Bun</option>

<option value="lotus">Lotus Seed Bun</option>

<option value="bean">Bean Paste Bun</option>

<option value="nest">Bird-Nest Bun</option>

</select>

<input type="submit" name="submit">

</form>

If the form in Example 6-4 is submitted with Chicken Bun and Bird-Nest Bun selected, then $_POST['lunch'] becomes a two-element array, with element values chicken and nest. Access these values using the regular multidimensional array syntax. Example 6-5 incorporates the form from Example 6-4 into a complete program that prints out each value selected in the menu. (The same rule applies here to the filename and the action attribute. Save the code in Example 6-5 in a file called eat.php or adjust the action attribute of the <form> tag to the correct filename.)

Example 6-5. Accessing multiple submitted values
<form method="POST" action="eat.php">

<select name="lunch[  ]" multiple>

<option value="pork">BBQ Pork Bun</option>

<option value="chicken">Chicken Bun</option>

<option value="lotus">Lotus Seed Bun</option>

<option value="bean">Bean Paste Bun</option>

<option value="nest">Bird-Nest Bun</option>

</select>

<input type="submit" name="submit">

</form>

Selected buns:

<br/>

<?php

foreach ($_POST['lunch'] as $choice) {

    print "You want a $choice bun. <br/>";

}

?>

With Chicken Bun and Bird-Nest Bun selected in the menu, Example 6-5 prints (after the form):

Selected buns:

You want a chicken bun.

You want a nest bun.

You can think of a form element named lunch[ ] as translating into the following PHP code when the form is submitted (assuming the submitted values for the form element are chicken and nest):

$_POST['lunch'][  ] = 'chicken';

$_POST['lunch'][  ] = 'nest';

As you saw in Example 4-5, this syntax adds an element to the end of an array.

    Previous Table of Contents Next