Question

This form contains many other inputs that work fine like <input>, <select> (not multiple) etc...I cannot figure out where exactly my problem is when I use <select multiple. I have the following code:

<form action="phpaction.php" method="post">
<select name="states[]" multiple>
<option value="AL">Alabama</option>
<option value="AK">Alaska</option>
...
</select>

// phpaction.php file
$statesArray=$_POST['states'];
print_r ($statesArray);

Why is not it working? I get the following error: Undefined index: states in phpaction.php on line 72

Was it helpful?

Solution

Try this:

<?php
if ($_SERVER['REQUEST_METHOD']==='POST') {
    $statesArray=$_POST['states'];
    print_r($statesArray);
}
?>

<form action="" method="post">
    <select name="states[]" multiple>
        <option value="AL">Alabama</option>
        <option value="AK">Alaska</option>
    </select>
    <input type="submit">
</form>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top