How to get multiple selected values of select box in php? - GeeksforGeeks

31 Aug, 2021
Given a list of items and the task is to retrieve the multiple selected value from a select box using PHP.Use multiple attribute in HTML to select multiple value from drop down list. Selecting multiple values in HTML depends on operating system and browser.  
For window users – hold down + CTRL key to select multiple option
For mac users – hold down command key to select multiple option
Example: This example creates a list of items using HTML.  
html
<html>    <body>        <form method = "post" action = "name.php">            <h4>SELECT SUBJECTS</h4>            <!--Using multiple to select multiple value-->            <select name = "subject" multiple size = 6>                 <option value = "english">ENGLISH</option>                <option value = "maths">MATHS</option>                <option value = "computer">COMPUTER</option>                <option value = "physics">PHYSICS</option>                <option value = "chemistry">CHEMISTRY</option>                <option value = "hindi">HINDI</option>            </select>            <input type = "submit" name = "submit" value = Submit>        </form>    </body></html>
Now, the task is to retrieve or print multiple selected value from list. Use form method and loop to retrieve selected value in PHP.Example:  
php
<html>    <body>        <!--name.php to be called on form submission-->        <form method = 'post'>            <h4>SELECT SUBJECTS</h4>                         <select name = 'subject[]' multiple size = 6>                 <option value = 'english'>ENGLISH</option>                <option value = 'maths'>MATHS</option>                <option value = 'computer'>COMPUTER</option>                <option value = 'physics'>PHYSICS</option>                <option value = 'chemistry'>CHEMISTRY</option>                <option value = 'hindi'>HINDI</option>            </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["subject"]))        {            // Retrieving each selected option            foreach ($_POST['subject'] as $subject)                print "You selected $subject<br/>";        }    else        echo "Select an option first !!";    }?>
Output:  
Note: The form can be submitted using $_GET method. It depends on the form method=”?” value. 
PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples.
 
rs1686740
Picked
PHP
PHP Programs
Technical Scripter
Web Technologies
PHP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
How to Insert Form Data into Database using PHP ?
How to convert array to string in PHP ?
How to check whether an array is empty using PHP?
How to receive JSON POST with PHP ?
Comparing two dates in PHP
How to Insert Form Data into Database using PHP ?
How to convert array to string in PHP ?
How to call PHP function on the click of a Button ?
How to check whether an array is empty using PHP?
Comparing two dates in PHP