Fri. Nov 8th, 2024

sarveshpathak.com

Code Snippets, Tutorials, Articles, Technical Stuff

How to Create Multi Select Dropdown in PHP

1 min read
How to Create Multi Select Dropdown in PHP 2

Multiple Select Dropdown

Here we will understand the following points on How to Create Multiple Select Dropdowns in PHP using these Easy Steps.

Step-1.  Bind Dropdown from the database
Step-2.  Add Attribute multiple in the Dropdown.
Step-3.  Create a Form Submit Code.

Complete Source Code for Multi Select Dropdown in PHP
<?php
define('DB_SERVER','localhost');
define('DB_USER','root');
define('DB_PASS' ,'');
define('DB_NAME', 'mis');
$con = mysqli_connect(DB_SERVER,DB_USER,DB_PASS,DB_NAME);
// Check connection
if (mysqli_connect_errno())
{
 echo "Failed to connect to MySQL: " . mysqli_connect_error();
}


?>
<html> 
    <body> 
        <!--name.php to be called on form submission--> 
        <form method = 'post'>  
            <h4>SELECT State</h4> 
              
            <select name = 'state[]' multiple size = 6>   
                 <option value="">- Choose State-</option>
														<?php $query=mysqli_query($con,"SELECT state_code, state_name FROM `mis_state`");
	$cnt=1;
	while($row=mysqli_fetch_array($query))
	{
		echo "<option value='$row[state_name]'> $row[state_name] </option>";
	}
	?>
	<?php  ?> 
            </select> 
            <input type = 'submit' name = 'submit' value = Submit> 
        </form> 
    </body> 
</html> 
<?php 
      
    // Check if form is submitted successfully 
    if(isset($_POST["submit"]))  
    { 
        // Check if any option is selected 
        if(isset($_POST["state"]))  
        { 
            // Retrieving each selected option 
            foreach ($_POST['state'] as $state)  
                print "You have selected  $state<br/>"; 
        } 
    else
        echo "Plese select any state !!"; 
    } 
?> 
Output Multi Select Dropdown in PHP
Multi Select Dropdown in PHP

Download Sourse Code

About Post Author

1 thought on “How to Create Multi Select Dropdown in PHP

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.