How can multiple selected options from a select element be read in PHP?
When multiple options are selected from a select element in HTML, they are sent as an array in the form of "name[]" to the server. In PHP, you can access these selected options by using the $_POST or $_GET superglobals depending on the form submission method. You can then loop through the array to process each selected option individually.
<?php
if(isset($_POST['options'])) {
$selectedOptions = $_POST['options'];
foreach($selectedOptions as $option) {
echo $option . "<br>";
}
}
?>
<form method="post">
<select name="options[]" multiple>
<option value="option1">Option 1</option>
<option value="option2">Option 2</option>
<option value="option3">Option 3</option>
</select>
<input type="submit" value="Submit">
</form>
Keywords
Related Questions
- What are the consequences of not binding the 'id' parameter in a database query in PHP?
- Are there any recommended tutorials or resources for implementing login systems and user groups in PHP using OOP principles?
- In what ways can a designer with limited PHP knowledge improve their understanding and implementation of PHP scripts for web development projects?