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

后端 未结 10 2022
既然无缘
既然无缘 2020-11-22 04:18

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\'. T

相关标签:
10条回答
  • 2020-11-22 04:40

    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;
    }
    ?>
    
    0 讨论(0)
  • 2020-11-22 04:48

    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);
    
    0 讨论(0)
  • 2020-11-22 04:50
    // 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"); 
             } 
             ...
    
             ...
        }
    ?>
    
    0 讨论(0)
  • 2020-11-22 04:52

    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.

    0 讨论(0)
  • 2020-11-22 04:52
    foreach ($_POST["select2"] as $selectedOption)
    {    
        echo $selectedOption."\n";  
    }
    
    0 讨论(0)
  • 2020-11-22 04:54
        <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"; 
    
    0 讨论(0)
提交回复
热议问题