如何从使用PHP的select标签$ _POST一个数组?

时间:2013-05-12 01:49:01

标签: php arrays select post

此表单包含许多其他输入正常工作,如<input>, <select> (not multiple) etc...当我使用<select multiple时,我无法确定问题的确切位置。我有以下代码:

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

为什么不工作?我收到以下错误:Undefined index: states in phpaction.php on line 72

1 个答案:

答案 0 :(得分:3)

试试这个:

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