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

后端 未结 10 2023
既然无缘
既然无缘 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:57

    This will display the selected values:

    <?php
    
        if ($_POST) { 
            foreach($_POST['select2'] as $selected) {
                echo $selected."<br>";
            }
        }
    
    ?>
    
    0 讨论(0)
  • 2020-11-22 04:58

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

    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.

    0 讨论(0)
  • 2020-11-22 04:59

    Change:

    <select name="select2" ...
    

    To:

    <select name="select2[]" ...
    
    0 讨论(0)
提交回复
热议问题