When dealing with a form that includes a select box with multiple selection enabled, obtaining the selected values in your PHP script can be straightforward. To cater to this specific scenario, we'll explore how to achieve this on the "display.php" page using $_GET[].
Form Structure with Multiple Selection:
Consider the following HTML form code, in which the select box named "select2" allows multiple selections:
<select name="select2[]" multiple ...> <option value="11">eleven</option> ... </select>
Notice the addition of square brackets [] to the name attribute. This modification is crucial for enabling PHP to treat $_GET['select2'] as an array of selected values.
Accessing the Values in PHP:
On the "display.php" page, you can access the selected values using the following PHP code:
foreach ($_GET['select2'] as $selectedOption) echo $selectedOption."\n";
By traversing through the $_GET['select2'] array, you can retrieve and display the individual selected options in a loop.
Additional Considerations:
By following these steps, you can effectively access and process multiple selected values of a select box in PHP, allowing you to handle user input in a flexible and dynamic manner.
The above is the detailed content of How to Retrieve Multiple Selected Values from a Select Box in PHP?. For more information, please follow other related articles on the PHP Chinese website!