我正在尝试使用复选框进行过滤。问题是当我取消选择一些盒子时 并提交值,再次选中所有框。我认为问题可以在这里
if (isset($_POST['option_meno']))
可能是什么问题? 是的,我是新来的,所以任何想让我的代码更简单的想法也会帮助我。
谢谢。
<?php
// Make a MySQL Connection
$query_o_meno = "SELECT meno FROM uctovnictvo GROUP BY meno ORDER BY meno";
$result_meno = mysql_query($query_o_meno) or die(mysql_error());
?>
<form method="post">
<?php
if (isset($_POST['Filtrovat'])) {
// Print checked checkboxes
echo "<strong>Meno:</strong>";
echo "<br />";
while($row = mysql_fetch_array($result_meno)){
//I believe the problem is line below -----------------------------
if (isset($_POST['option_meno'])) {
echo "<input type='checkbox' name='option_meno[]' value='".$row['meno']."' checked />" . $row['meno'];
echo "<br />";
//}
}
else{
echo "<input type='checkbox' name='option_meno[]' value='".$row['meno']."' / >" . $row['meno'];
echo "<br />";
}
}
}
else{
$option_meno = array();
echo "<strong>Meno:</strong>";
echo "<br />";
while($row = mysql_fetch_array($result_meno)){
echo "<input type='checkbox' name='option_meno[]' value='".$row['meno']."' checked />" . $row['meno'];
echo "<br />";
$option_meno[] = $row['meno'];
}
}
?>
<input type="submit" name="Filtrovat" value="Filtrovat" />
</form>
答案 0 :(得分:0)
由于您正在执行name='option_meno[]'
,$_POST['option_meno']
将是一个数组。只要至少选中一个复选框,就会始终设置该数组。尝试这样的事情:
<?php
// Make a MySQL Connection
$query_o_meno = "SELECT meno FROM uctovnictvo GROUP BY meno ORDER BY meno";
$result_meno = mysql_query($query_o_meno) or die(mysql_error());
?>
<form method="post">
<?php
if (isset($_POST['Filtrovat'])) {
// Print checked checkboxes
echo "<strong>Meno:</strong>";
echo "<br />";
while($row = mysql_fetch_array($result_meno)){
//I believe the problem is line below -----------------------------
if (isset($_POST['option_meno'][$row['meno']])) {
echo "<input type='checkbox' name='option_meno[".$row['meno']."]' value='".$row['meno']."' checked='checked' />" . $row['meno'];
echo "<br />";
//}
}
else{
echo "<input type='checkbox' name='option_meno[".$row['meno']."]' value='".$row['meno']."' / >" . $row['meno'];
echo "<br />";
}
}
}
else{
$option_meno = array();
echo "<strong>Meno:</strong>";
echo "<br />";
while($row = mysql_fetch_array($result_meno)){
echo "<input type='checkbox' name='option_meno[".$row['meno']."]' value='".$row['meno']."' checked />" . $row['meno'];
echo "<br />";
$option_meno[] = $row['meno'];
}
}
?>
<input type="submit" name="Filtrovat" value="Filtrovat" />
</form>
这样您就可以单独检查每个复选框。
注意:您应该始终完全声明checked属性,如下所示:checked='checked'
答案 1 :(得分:0)
这未经过测试,但应该有效:
if (isset($_POST['Filtrovat']))
{
// Print checked checkboxes
echo "<strong>Meno:</strong>";
echo "<br />";
while($row = mysql_fetch_array($result_meno))
{
$checked = "";
if(isset($_POST['option_meno']))
$checked = in_array($row['meno'],$_POST['option_meno'])?"
checked = 'checked' ":"";
echo "<input type='checkbox' name='option_meno[]'
value='".$row['meno']."' $checked / >" . $row['meno'];
echo "<br />";
}
}