Implementing Multiple Select Box Value Retrieval in PHP
This question explores a scenario where an HTML form includes a select box with the multiple attribute enabled, allowing users to select multiple values. The goal is to retrieve these selected values on the display.php page using the $_GET[] array.
Solution:
To enable PHP to treat $_GET['select2'] as an array of options, you need to add square brackets to the name of the select element. Here's a modified version of your code:
<select name="select2[]" multiple ...>
This change enables PHP to interpret the selected options as an array. You can then access it in your PHP script using the following code:
header("Content-Type: text/plain"); foreach ($_GET['select2'] as $selectedOption) echo $selectedOption."\n";
This code iterates through the selected options and prints each one on a new line. Note that you can substitute $_GET with $_POST depending on the value of the form method attribute.
Key Point:
The key takeaway here is that adding square brackets to the select element name in the HTML form allows you to access the selected values as an array in your PHP script, making it easier to process them.
The above is the detailed content of How Can I Retrieve Multiple Select Box Values in PHP using $_GET?. For more information, please follow other related articles on the PHP Chinese website!