我正在尝试使用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>';
?>
答案 0 :(得分:2)
您应该post an array(请注意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>';
}
答案 1 :(得分:0)
你必须通过复选框作为数组将名称更改为pal_num []
答案 2 :(得分: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>';
?>
使用此代码