[php] How to get multiple selected values of select box in php?

I have a html form which has a select list box from which you can select multiple values because its multiple property is set to multiple. Consider form method is 'GET'. The html code for the form is as follows:

_x000D_
_x000D_
<html>_x000D_
    <head>_x000D_
    <title>Untitled Document</title>_x000D_
    </head>_x000D_
    <body>_x000D_
    <form id="form1" name="form1" method="get" action="display.php">_x000D_
      <table width="300" border="1">_x000D_
        <tr>_x000D_
          <td><label>Multiple Selection </label>&nbsp;</td>_x000D_
          <td><select name="select2" size="3" multiple="multiple" tabindex="1">_x000D_
            <option value="11">eleven</option>_x000D_
            <option value="12">twelve</option>_x000D_
            <option value="13">thirette</option>_x000D_
            <option value="14">fourteen</option>_x000D_
            <option value="15">fifteen</option>_x000D_
            <option value="16">sixteen</option>_x000D_
            <option value="17">seventeen</option>_x000D_
            <option value="18">eighteen</option>_x000D_
            <option value="19">nineteen</option>_x000D_
            <option value="20">twenty</option>_x000D_
          </select>_x000D_
          </td>_x000D_
        </tr>_x000D_
        <tr>_x000D_
          <td>&nbsp;</td>_x000D_
          <td><input type="submit" name="Submit" value="Submit" tabindex="2" /></td>_x000D_
        </tr>_x000D_
      </table>_x000D_
    </form>_x000D_
    </body>_x000D_
    </html>
_x000D_
_x000D_
_x000D_

I want to display the selected values in select list box on display.php page. So how are the selected values accessed on display.php page using $_GET[] array.

This question is related to php html drop-down-menu combobox html-table

The answer is


You can use this code to retrieve values from multiple select combo box

HTML:

<form action="c3.php" method="post">
  <select name="ary[]" multiple="multiple">
    <option value="Option 1" >Option 1</option>
    <option value="Option 2">Option 2</option>
    <option value="Option 3">Option 3</option>
    <option value="Option 4">Option 4</option>
    <option value="Option 5">Option 5</option>
  </select>
  <input type="submit">
</form>

PHP:

<?php
$values = $_POST['ary'];

foreach ($values as $a){
    echo $a;
}
?>

This will display the selected values:

<?php

    if ($_POST) { 
        foreach($_POST['select2'] as $selected) {
            echo $selected."<br>";
        }
    }

?>

    <html>
<head>
<title>Untitled Document</title>
</head>
<body>
<form id="form1" name="form1" method="get" action="display.php">
  <table width="300" border="1">
    <tr>
      <td><label>Multiple Selection </label>&nbsp;</td>
      <td><select name="select2[]" size="3" multiple="multiple" tabindex="1">
        <option value="11">eleven</option>
        <option value="12">twelve</option>
        <option value="13">thirette</option>
        <option value="14">fourteen</option>
        <option value="15">fifteen</option>
        <option value="16">sixteen</option>
        <option value="17">seventeen</option>
        <option value="18">eighteen</option>
        <option value="19">nineteen</option>
        <option value="20">twenty</option>
      </select>
      </td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td><input type="submit" name="Submit" value="Submit" tabindex="2" /></td>
    </tr>
  </table>
</form>
</body>
</html>

You can iterate it directly like this

foreach ($_GET['select2'] as $value)
    echo $value."\n";

or you can do it like this

$selectvalue=$_GET['select2'];
foreach ($selectvalue as $value)
    echo $value."\n"; 

I fix my problem with javascript + HTML. First i check selected options and save its in a hidden field of my form:

for(i=0; i < form.select.options.length; i++)
   if (form.select.options[i].selected)
    form.hidden.value += form.select.options[i].value;

Next, i get by post that field and get all the string ;-) I hope it'll be work for somebody more. Thanks to all.


// CHANGE name="select2" TO name="select2[]" THEN
<?php
  $mySelection = $_GET['select2'];

  $nSelection = count($MySelection);

  for($i=0; $i < $nSelection; $i++)
   {
      $numberVal = $MySelection[$i];

        if ($numberVal == "11"){
         echo("Eleven"); 
         }
        else if ($numberVal == "12"){
         echo("Twelve"); 
         } 
         ...

         ...
    }
?>

Use the following program for select the multiple values from select box.

multi.php

<?php
print <<<_HTML_
<html>
        <body>
                <form method="post" action="value.php">
                        <select name="flower[ ]" multiple>
                                <option value="flower">FLOWER</option>
                                <option value="rose">ROSE</option>
                                <option value="lilly">LILLY</option>
                                <option value="jasmine">JASMINE</option>
                                <option value="lotus">LOTUS</option>
                                <option value="tulips">TULIPS</option>
                        </select>
                        <input type="submit" name="submit" value=Submit>
                </form>
        </body>
</html>
_HTML_

?>

value.php

<?php
foreach ($_POST['flower'] as $names)
{
        print "You are selected $names<br/>";
}

?>

Change:

<select name="select2" ...

To:

<select name="select2[]" ...

You could do like this too. It worked out for me.

<form action="ResultsDulith.php" id="intermediate" name="inputMachine[]" multiple="multiple" method="post">
    <select id="selectDuration" name="selectDuration[]" multiple="multiple"> 
        <option value="1 WEEK" >Last 1 Week</option>
        <option value="2 WEEK" >Last 2 Week </option>
        <option value="3 WEEK" >Last 3 Week</option>
         <option value="4 WEEK" >Last 4 Week</option>
          <option value="5 WEEK" >Last 5 Week</option>
           <option value="6 WEEK" >Last 6 Week</option>
    </select>
     <input type="submit"/> 
</form>

Then take the multiple selection from following PHP code below. It print the selected multiple values accordingly.

$shift=$_POST['selectDuration'];

print_r($shift);

foreach ($_POST["select2"] as $selectedOption)
{    
    echo $selectedOption."\n";  
}

Examples related to php

I am receiving warning in Facebook Application using PHP SDK Pass PDO prepared statement to variables Parse error: syntax error, unexpected [ Preg_match backtrack error Removing "http://" from a string How do I hide the PHP explode delimiter from submitted form results? Problems with installation of Google App Engine SDK for php in OS X Laravel 4 with Sentry 2 add user to a group on Registration php & mysql query not echoing in html with tags? How do I show a message in the foreach loop?

Examples related to html

Embed ruby within URL : Middleman Blog Please help me convert this script to a simple image slider Generating a list of pages (not posts) without the index file Why there is this "clear" class before footer? Is it possible to change the content HTML5 alert messages? Getting all files in directory with ajax DevTools failed to load SourceMap: Could not load content for chrome-extension How to set width of mat-table column in angular? How to open a link in new tab using angular? ERROR Error: Uncaught (in promise), Cannot match any routes. URL Segment How to implement drop down list in flutter? How can I create a dropdown menu from a List in Tkinter? How can I close a dropdown on click outside? Making a drop down list using swift? HTML: Select multiple as dropdown How to get selected value of a dropdown menu in ReactJS Avoid dropdown menu close on click inside Bootstrap 3 dropdown select How to make a drop down list in yii2? Android custom dropdown/popup menu

Examples related to combobox

How to set combobox default value? PHP code to get selected text of a combo box How to add items to a combobox in a form in excel VBA? How add items(Text & Value) to ComboBox & read them in SelectedIndexChanged (SelectedValue = null) Get Selected value of a Combobox jQuery "on create" event for dynamically-created elements How to get the selected item of a combo box to a string variable in c# HTML combo box with option to type an entry twitter bootstrap autocomplete dropdown / combobox with Knockoutjs C# winforms combobox dynamic autocomplete

Examples related to html-table

Table column sizing DataTables: Cannot read property 'length' of undefined TypeError: a bytes-like object is required, not 'str' in python and CSV How to get the <td> in HTML tables to fit content, and let a specific <td> fill in the rest How to stick table header(thead) on top while scrolling down the table rows with fixed header(navbar) in bootstrap 3? Sorting table rows according to table header column using javascript or jquery How to make background of table cell transparent How to auto adjust table td width from the content bootstrap responsive table content wrapping How to print table using Javascript?