-1

我正在尝试使用 POST 方法从输入表单(复选框)传递多个值,但只有一个值被转储,无论检查了多少个复选框。我做错了什么?

var_dump($_POST);

结果是:array(2) { ["pal_num"]=> string(1) "2" ["post"]=> string(3) "Go!" }

代码:

<?php
$l = $_POST['LT'];
$pals = '';

$r = mysql_query("SELECT DISTINCT pal_num FROM pl_tab WHERE lt_num='$l'");

while($row = mysql_fetch_assoc($r))
{
    $pals .= '<input type="checkbox" name="pal_num" value="'.$row['pal_num'].'">'.$row['pal_num'].'<br>';
}

if($pal == '')
    echo '';
else
echo '<form name="get_pal" action="post.php" method="POST">';
echo $pals;
echo '<input type="submit" name="post" value="Go!">';
echo '</form>';
?>
4

3 回答 3

2

你应该发布一个数组(注意后面的方括号pal_num

$pals .= '<input type="checkbox" name="pal_num[]" value="'.$row['pal_num'].'">'.$row['pal_num'].'<br>';

此外,您的if构造不正确,您应该使用括号:

if($pal == '') {
    echo '';
} else {
    echo '<form name="get_pal" action="post.php" method="POST">';
    echo $pals;
    echo '<input type="submit" name="post" value="Go!">';
    echo '</form>';
}
于 2013-08-26T09:56:54.440 回答
0

您必须将复选框作为数组传递,将名称更改为 pal_num[]

于 2013-08-26T09:55:37.173 回答
0
<?php
var_dump($_POST)
array(2) { ["pal_num"]=> string(1) "2" ["post"]=> string(3) "Go!" }

<?php
$l = $_POST['LT'];
$pals = '';

$r = mysql_query("SELECT DISTINCT pal_num FROM pl_tab WHERE lt_num='$l'");

while($row = mysql_fetch_assoc($r))
{
    $pals .= '<input type="checkbox" name="pal_num[]" value="'.$row['pal_num'].'">'.$row['pal_num'].'<br>';
}

if($pal == '')
    echo '';
else
echo '<form name="get_pal" action="post.php" method="POST">';
echo $pals;
echo '<input type="submit" name="post" value="Go!">';
echo '</form>';
?>

使用此代码

于 2013-08-26T10:03:42.893 回答