$ POST_从多个复选框返回数组

时间:2012-11-26 01:33:41

标签: php html

您好我已经查看了其他答案,并且他们说要添加name = checkbox []以使数组返回但它似乎无法正常工作。

HTML是:

<select class="select" multiple="multiple" name="suburb[]" id="suburb">
<option selected="selected" name="suburb[]" value="Southbank">Southbank</option>
<option selected="selected" name="suburb[]" value="Melbourne">Melbourne</option>
<option selected="selected" name="suburb[]" value="Docklands">Docklands</option>
<option selected="selected" name="suburb[]" value="South Melbourne">South Melbourne</option>
<option selected="selected" name="suburb[]" value="West Melbourne">West Melbourne</option>
<option selected="selected" name="suburb[]" value="Point Cook">Point Cook</option>
<option selected="selected" name="suburb[]" value="Sanctuary Lakes">Sanctuary Lakes</option>
<option selected="selected" name="suburb[]" value="Truganina">Truganina</option>
<option selected="selected" name="suburb[]" value="Williams Landing">Williams Landing</option>

PHP代码是:

    $message .= "<tr><td><strong>Interested Suburbs:</strong> </td><td>" . strip_tags($POST_['suburb']) . "</td></tr>";

3 个答案:

答案 0 :(得分:3)

$_POST['suburb']

数组不是字符串也是你把它拼错了它的$ _POST,所以你需要循环遍历它就像这样发布:

$message .= "<tr><td><strong>Interested Suburbs:</strong> </td><td>";

foreach ($_POST['suburb'] as $suburb)
{
    $message .= strip_tags($suburb) . "<br />\n";
}

$message .= "</td></tr>";

答案 1 :(得分:2)

正常工作:您只需要使用数组的$_POSTprint_r来查看其内容。试试这个,你会发现它运作正常。

顺便说一下,name

上不需要options属性
<form method="post" action="<?=$PHP_SELF?>">
    <select name="suburb[]" class="select" multiple="multiple" id="suburb">
        <option selected="selected" value="Southbank">Southbank</option>
        <option selected="selected" value="Melbourne">Melbourne</option>
        <option selected="selected" value="Docklands">Docklands</option>
        <option selected="selected" value="South Melbourne">South Melbourne</option>
        <option selected="selected" value="West Melbourne">West Melbourne</option>
        <option selected="selected" value="Point Cook">Point Cook</option>
        <option selected="selected" value="Sanctuary Lakes">Sanctuary Lakes</option>
        <option selected="selected" value="Truganina">Truganina</option>
        <option selected="selected" value="Williams Landing">Williams Landing</option>
    </select>
    <button type="submit">test</button>
</form>
<?php
print_r($_POST['suburb']);
?>

答案 2 :(得分:1)

您要查找的变量是$_POST,而不是$POST_;有关详细信息,请参阅the PHP reference on $_POST

否则,从你所展示的内容来看,这可能很好。如果事情仍然不起作用,也要为表单标记添加HTML。