A simple select box without multiple attribute is used to select one option from the select list. There are cases where we want to select more than one option from the select list and that's where the multiple attribute of select box comes in handy. An html select box with multiple attribute specified is given below:
<select multiple>
<option>January</option>
<option>February</option>
<option>March</option>
<option>April</option>
<option>May</option>
<option>June</option>
<option>July</option>
<option>August</option>
<option>September</option>
<option>October</option>
<option>November</option>
<option>December</option>
</select>
How to get the multiple values selected in the select box using php?
We can get the multiple values selected in the select box by keeping the name attribute of the select box equal to an array i.e (name="month[]") and then sending the data to PHP using an html form. The example code of an html form having a select box with multiple attribute is given below:Html Code:
<form method="GET" action="">
<select name="month[]" multiple>
<option>January</option>
<option>February</option>
<option>March</option>
<option>April</option>
<option>May</option>
<option>June</option>
<option>July</option>
<option>August</option>
<option>September</option>
<option>October</option>
<option>November</option>
<option>December</option>
</select><br />
<input type="submit" name="submit" />
</form>
The above html form will send the selected data to php via get method which can be easily fetched from the GET Array in PHP. The PHP code to get and print the selected months is given below:
PHP Code:
<?php
if(isset($_GET['submit'])){
$months = $_GET['month'];
print_r($months);
}
?>
For more lessons subscribe on InformationBitz.com
No comments:
Post a Comment