2

嗨,我查看了其他答案,他们说要添加 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>";
4

3 回答 3

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>";
于 2012-11-26T01:38:25.747 回答
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']);
?>
于 2012-11-26T01:38:22.033 回答
1

您要查找的变量是$_POST,而不是$POST_; 有关更多详细信息,请参阅$_POST 上的 PHP 参考。

否则,从您所显示的内容来看,它可能没问题。如果仍然无法正常工作,请为您的表单标签添加 HTML。

于 2012-11-26T01:39:02.727 回答