Friday, July 19, 2019

How to get select the multiple values of drop down or select box in php?

I have a html and php form which you can select multiple values from drop down list.
To select multiple options in a drop-down list, we need to add the "multiple" attribute to <select> element.

For select the multiple option need to hold down below keys for Window and Mac users :
  •  For window - hold down Ctrl key to select more than one option
  •  For mac – hold down Command key to select more than one option

Example: This example create a drop down list using HTML form.

<html>
    <head>
    <title>Multiple Selection</title>
    </head>
    <body>
        <form name = "state" method = "post" action = "states.php"> 
            <h3>Select States</h3> 
            <select name = "states" multiple size = "8">   
               <option value="Maharashtra">Maharashtra</option>
                <option value="Delhi">Delhi</option>
<option value="Goa">Goa</option>
<option value="Gujarat">Gujarat</option>
<option value="Assam">Assam</option>
<option value="Bihar">Bihar</option>
<option value="Punjab">Punjab</option>
<option value="Rajasthan">Rajasthan</option>
            </select> 
            <input type = "submit" name = "submit" value = Submit> 
        </form> 
    </body> 
</html> 

Output : 

Example: This example create a drop down list using PHP and HTML. Please create states.php file.

<html>
    <head>
    <title>Multiple Selection</title>
    </head>

    <?php       
    // Check the form submission
    if(isset($_POST["submit"]))  
    { 
// Check if any option is selected 
if(isset($_POST["states"]))  
              foreach ($_POST['states'] as $states)  
echo "You selected : $states<br/>"; 
    else
echo "Please select an option first !"; 
    } 
   ?> 

    <body>
        <form name = "state" method = "post" action = "states.php"> 
            <h3>Multiple States</h3> 
            <select name = "states[]" multiple size = "8">   
               <option value="Maharashtra">Maharashtra</option>
                <option value="Delhi">Delhi</option>
<option value="Goa">Goa</option>
<option value="Gujarat">Gujarat</option>
<option value="Assam">Assam</option>
<option value="Bihar">Bihar</option>
<option value="Punjab">Punjab</option>
<option value="Rajasthan">Rajasthan</option>
            </select> 
            <input type = "submit" name = "submit" value = Submit> 
        </form> 
    </body> 
</html> 

Output :



















Please see here is showing your selected multiple options. Thats it !!


No comments:

Post a Comment

Popular Posts