README
TLDR Code Result
php html
Written: Jun-2023

Handling forms posted with multiple select options

This is a walk through of the PHP & HTML code required for handling forms that have been posted with a <select> that have the 'multiple' attribute.

The live sample below has results that highlight the problems that can be encountered with the two different options for how to handle a post, namely that if you submit a form with a multiple select with a standard non-array name (i.e. name='abc'), then you get a single _GET result. Whereas if you submit a form with an select named as an array (i.e. name='abc[]') then you will get all the selected options posted within an array (i.e. $_GET['abc'] will result in an //array// of values.

TLDR

Use the following code:

html :: Simple get action form with <select>
<form method='get' action='script.php'>   //(note, get and post work the same)//
    <select name='abc[]' multiple>
        <option>value</option>
    </select>
</form>

php
$vArr=$_GET['abc'];
foreach($vArr as $v) echo '<LI>$v</LI>';

Code

The relevant code can be found here:

forms-with-multiple-select-options.php
echo "<LI>url: <div class='highlighted' style='margin-left:50px;'>".$_SERVER['REQUEST_URI']."</div>";
echo "<BR><LI>PHP \$_GET result:<div class='highlighted' style='margin-left:50px; white-space:pre-wrap'>";
echo $this->getVisibleHTML(print_r($_GET, true));

// ... and to further illustrate this....
if ( isset($_GET['myselect']) and is_array($_GET['myselect'])) {
    foreach($_GET['myselect'] as $v) echo "<BR>myselect array entry: [$v]";
}
echo "</div>";
forms-with-multiple-select-options.php
$script = $_SERVER['SCRIPT_NAME'];
echo "<table>";
if ( 1 ) {
    echo "<tr>";
    if (1) {
        echo "<td>";
        if (1) {
            echo "<p>The left side form posts a select named 'myselect' and results in each chosen value being posted with the same name ('myselect')</p>";
            echo "<form method='get' action='$script' name='myform'>";
            if (1) {
                echo "<input type='text' name='version' value='<select name='myselect' multiple>...' readonly><br>";
                echo "<select name='myselect' multiple>";
                if (1) {
                    echo "<option selected value='apples'>Apples</option>";
                    echo "<option selected value='oranges'>Oranges</option>";
                    echo "<option selected value='bananas'>Bananas</option>";
                    echo "<option selected value='kiwi'>Kiwi</option>";
                    echo "<option selected value='passion fruit'>Passion Fruit</option>";
                    echo "<option selected value='mango'>Mango</option>";
                }
                echo "</select>";

                echo "<br><button id='mysubmit' type='submit'>Submit...</button>"; // This button will submit the form
            }
            echo "</form>";
        }


        echo "</td>";
        echo "<td>";


        if (1) {
            echo "<p>The right side form posts a select named 'myselect[]' and results in an each chosen values being posted within an array named 'myselect'</p>";
            echo "<form method='get' action='$script' name='myform'>";
            if (1) {
                echo "<input type='text' name='version' value='<select name='myselect[]' multiple>...' readonly><br>";
                echo "<select name='myselect[]' multiple>";
                if (1) {
                    echo "<option selected value='apples'>Apples</option>";
                    echo "<option selected value='oranges'>Oranges</option>";
                    echo "<option selected value='bananas'>Bananas</option>";
                    echo "<option selected value='kiwi'>Kiwi</option>";
                    echo "<option selected value='passion fruit'>Passion Fruit</option>";
                    echo "<option selected value='mango'>Mango</option>";
                }
                echo "</select>";

                echo "<br><button id='mysubmit' type='submit'>Submit...</button>"; // This button will submit the form
            }
            echo "</form>";
        }
        echo "</td>";
    }
    echo "</tr>";
}
echo "</table>";

Result


link Click to view a demo

square