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

PhpHtmlDrop Down-MenuComboboxHtml Table

Php Problem Overview


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.

Php Solutions


Solution 1 - Php

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.

Solution 2 - Php

Change:

<select name="select2" ...

To:

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

Solution 3 - Php

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;
}
?>

Solution 4 - Php

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

?>

Solution 5 - Php

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

Solution 6 - Php

This will display the selected values:

<?php

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

?>

Solution 7 - Php

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

         ...
    }
?>

Solution 8 - Php

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

Solution 9 - Php

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.

Solution 10 - Php

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionParam-GanakView Question on Stackoverflow
Solution 1 - PhpAlex JasminView Answer on Stackoverflow
Solution 2 - PhpCoufuView Answer on Stackoverflow
Solution 3 - PhpAbdul Kalam AzadView Answer on Stackoverflow
Solution 4 - Phprekha_sriView Answer on Stackoverflow
Solution 5 - PhpVivekView Answer on Stackoverflow
Solution 6 - PhpahmedView Answer on Stackoverflow
Solution 7 - PhpRynikaView Answer on Stackoverflow
Solution 8 - PhpDulacosteView Answer on Stackoverflow
Solution 9 - PhpDrakoView Answer on Stackoverflow
Solution 10 - PhpSwRView Answer on Stackoverflow