As you have seen in our processing script we collect the option selected by user by using appropriate GET or POST methods. Here is one example. Let us say the list box of colors we have used and the option selected by user is collected like this
$color=$_POST['color'];
or
$color=$_GET['color'];
Based on the form method ( GET or POST ) we can get or collect the option selected and store them in variable $color.
Same way let us see how we can get multiple selections from a drop down list box. Here is the demo of multiple selection pull down list box.
Here we get the selected values as array ( not as a single value ) , now this array we can loop through and display the elements. Here is the code to display the list box and then the collection of selected options.
Press and hold the Ctrl key and then click on the options to select multiple.
Here is the code to handle the above script.
<form method=post action=''>
<select name='color[]' size=4 multiple>
<option value='blue'>Blue</option>
<option value='green'>Green</option>
<option value='red'>Red</option>
<option value='yellow'>Yelllow</option>
<option value='' selected>Select a Color </option>
<option value='white'>White</option>
</select>
<input type=submit></form>
///// collecting form data /////
@$color= $_POST['color'];
if( is_array($color)){
while (list ($key, $val) = each ($color)) {
echo "$val <br>";
}
}//
else{echo "not array";}
Empty array
Sometime before using the array we will find out if any element is present or not. We don't want to use one empty array for further processing.
if( !empty($color[0]) ){
////
// Your code here
///
}