At the beginning of every request, the PHP engine 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
- $_GET[‘category’] is set to fryingpan
Submitting the form in Example 7-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 7-2. A two-element form
<form method=”POST” action=”catatog.php”>
<input type=”text” name=”product_id”>
<select name=”category”>
<option vatue=”ovenmitt”>Pot Hotder</option>
<option vatue=”fryingpan”>Frying Pan</option>
<option vatue=”torch”>Kitchen Torch</option>
</select>
<input type=”submit” name=”submit”>
</form>
Example 7-3 incorporates the form in Example 7-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 7-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 7-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’] ?? ” ?>
To avoid a warning message from PHP when no POST variables have been submitted, Example 7-3 uses ??, the null coalesce operator.
The code $_POST[‘product_id’ ] ?? ‘ ‘ evaluates to whatever’s in $_POST[‘pro duct_id’] if there’s something there, or the empty string (”) otherwise. Without it, you’d see messages like PHP Notice: Undefined index: product_id when the page is retrieved by the GET method and no POST variables have been set up.
A form element that can have multiple values needs to have a name that ends in []. This tells the PHP engine to treat the multiple values as array elements. The <select> menu in Example 7-4 has its submitted values put into $_POST[‘lunch’].
Example 7-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 7-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 7-5 incorporates the form from Example 7-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 7-5 in a file called eat.php or adjust the action attribute of the <form> tag to the correct filename.)
Example 7-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
if (isset($_POST[‘lunch’])) {
foreach ($_POST[‘lunch’] as $choice) {
print “You want a $choice bun. <br/>”;
}
}
?>
With Chicken Bun and Bird-Nest Bun selected in the menu, Example 7-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-6, this syntax adds an element to the end of an array.
Source: Sklar David (2016), Learning PHP: A Gentle Introduction to the Web’s Most Popular Language, O’Reilly Media; 1st edition.