271

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:

<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>

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.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Param-Ganak
  • 5,787
  • 17
  • 50
  • 62

11 Answers11

426

If you want PHP to treat $_GET['select2'] as an array of options just add square brackets to the name of the select element like this: <select name="select2[]" multiple …

Then you can acces the array in your PHP script

<?php
header("Content-Type: text/plain");

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

$_GET may be substituted by $_POST depending on the <form method="…" value.

Alex Jasmin
  • 39,094
  • 7
  • 77
  • 67
  • 1
    for me it seems, that browser does not send the post/get-parameter if nothing was selected of the multiple-select. how can you force to have an empty array, instead? – emfi Mar 11 '15 at 13:16
  • I had to use $_POST['select2'] instead of $_GET['select2'] – Kyle Bridenstine Apr 21 '15 at 05:05
  • 3
    This is an old answer, but this is a misleading answer! (name="select2[]") is correct as @Coufu answered! – meYnot Jan 09 '16 at 16:41
  • @emfi If you want the parameter to be sent there must be a value; you can add "selected" to the tag (ie ``) if you can tolerate having a default option in the list. You can also use CSS to make the default option invisible; however if someone selects a different option, then unselects everything.. you can wind up with no value being returned. So.. there's no clean solution using GET. – apraetor Mar 05 '17 at 22:40
  • 1
    Adding [] works for me both on POST and GET, but the resulting URI (with GET) is just too awful (..multiselect.php?ms%5B%5D=1&ms%5B%5D=2) – Teson May 03 '17 at 05:49
200

Change:

<select name="select2" ...

To:

<select name="select2[]" ...
Coufu
  • 2,031
  • 1
  • 11
  • 4
45

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;
}
?>
Kobi
  • 135,331
  • 41
  • 252
  • 292
35

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/>";
}

?>
rekha_sri
  • 2,677
  • 1
  • 24
  • 27
8
    <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"; 
Vivek
  • 1,446
  • 18
  • 27
5

This will display the selected values:

<?php

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

?>
Gigala
  • 143
  • 2
  • 10
ahmed
  • 75
  • 1
  • 1
  • 4
    This has multiple flaws: 1) the OP was using GET method; 2) it's lacking the most important step of appending square brackets to the form element name, like `name="select2[]"`. – charlie Mar 18 '15 at 15:50
5
// 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"); 
         } 
         ...

         ...
    }
?>
Rynika
  • 51
  • 1
  • 2
4

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);
Du-Lacoste
  • 11,530
  • 2
  • 71
  • 51
0

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.

Drako
  • 769
  • 1
  • 8
  • 23
  • 1
    This has multiple flaws: 1) is depending on the JavaScript being available; 2) does not separate the values when joining them into a single string, thus making it imposible to split the string back into values later on. – charlie Mar 18 '15 at 15:43
  • This answer gives me the necessary direction. Thx. – Oleg Popov Jul 18 '17 at 03:38
0
foreach ($_POST["select2"] as $selectedOption)
{    
    echo $selectedOption."\n";  
}
SwR
  • 612
  • 1
  • 8
  • 21
-1

form submit with enctype="multipart/form-data"

Ehsan Gholami
  • 85
  • 2
  • 5