如何使用会话数组检查复选框
Posted
技术标签:
【中文标题】如何使用会话数组检查复选框【英文标题】:How to check the checkbox using session array 【发布时间】:2017-01-23 13:43:30 【问题描述】:我是 php 新手,我已经在复选框上工作了好几天,但仍然无法解决它。我试图通过使用 0 和 1 作为会话中的状态来检查复选框。问题是提交后它不会显示已检查,但实际上已在会话中检查。 在 POST 中,我使用 window.location.href 来刷新页面,以便复选框显示为选中状态。如果我没有使用 window.location.href,就像我上面提到的那样,复选框被选中,但在第一次单击按钮后不显示。当我再次单击该按钮时,该复选框将显示为已选中,并且会话状态也已选中,其他复选框也是如此。
<form name="filter" method="POST" action="">
<div class="panel-body">
<div class="form-group">
<?php
$check_array = array();
$search = $_GET['search'];
$category = mysqli_query($conn, "SELECT * FROM categories");
$numrow = mysqli_num_rows($category);
$i = 0;
while($crow = mysqli_fetch_assoc($category))
array_push($check_array, [$crow['categories_id'], $crow['categories_name'],$_SESSION['cat'][$i][2]]);
if(empty($_SESSION['cat'][$i][2]))
?>
<script>
$(document).ready(function()
$('#categories_id<?php echo $crow['categories_id']; ?>').attr('checked', false););
</script>
<?php
elseif($_SESSION['cat'][$i][2] == 0)
?>
<script>
$(document).ready(function()
$('#categories_id<?php echo $crow['categories_id']; ?>').attr('checked', false););
</script>
<?php
elseif($_SESSION['cat'][$i][2] == 1)
?>
<script>
$(document).ready(function()
$('#categories_id<?php echo $crow['categories_id']; ?>').attr('checked', true););
</script>
<?php
?>
<div class="checkbox">
<label><input type="checkbox" name="categories_id<?php echo $crow['categories_id']; ?>" id="categories_id<?php echo $crow['categories_id']; ?>"><?php echo $crow['categories_name']; ?>
<input type="hidden" name="catname<?php echo $crow['categories_id']; ?>" id="<?php echo $crow['categories_id']; ?>" value="<?php echo $crow['categories_id']; ?>">
</label>
</div>
<?php
$i++;
?>
</div>
</div><button type="submit" class="btn btn-default" name="Btn">Filter</button></form>
这是我的 POST 函数
if(isset($_POST['Btn']))
for($j=0; $j<count($check_array); $j++)
$_SESSION['cat'][$j][0] = $check_array[$j][0];
$_SESSION['cat'][$j][1] = $check_array[$j][1];
for($k=0;$k<count($check_array);$k++)
$val = $_POST['catname'.$check_array[$k][0]];
if(isset($_POST['categories_id'.$check_array[$k][0]]))
$selected = 1;
else
$selected = 0;
if($_SESSION['cat'][$k][0] == $val)
$_SESSION['cat'][$k][2] = $selected;
?>
<script>
window.location = window.location.href;</script>
<?php
下面是数组的输出
Array ( [0] => 21 [1] => Accessories [2] => 1 )
Array ( [0] => 13 [1] => Outdoor sports [2] => 0 )
Array ( [0] => 2 [1] => Women [2] => 0 )
Array ( [0] => 10 [1] => Yard, garden & outdoor [2] => 0 )
第一个数字是类别id,后面是类别名称,最后一个是当复选框被选中时它会变成1 else 0
请给我建议。
【问题讨论】:
我认为最后一行是错误的“ 嗯,我相信你提到的错误只是我的编辑技巧不好。我确信没有像缺少右括号这样的语法错误,因为我已经计算出括号的数量是平衡的:) 【参考方案1】:Please try :
if(isset($_POST['Btn']))
for($j=0; $j<count($check_array); $j++)
$_SESSION['cat'][$j][0] = $check_array[$j][0];
$_SESSION['cat'][$j][1] = $check_array[$j][1];
for($k=0;$k<count($check_array);$k++)
$val = $_POST['catname'.$check_array[$k][0]];
if(isset($_POST['categories_id'.$check_array[$k][0]]))
$selected = 1;
$_SESSION['checkedIn'] = true;
else
$selected = 0;
$_SESSION['checkedIn'] = false;
if($_SESSION['cat'][$k][0] == $val)
$_SESSION['cat'][$k][2] = $selected;
?>
and in html
<input type="checkbox"
<?php if($_SESSION['checkedIn']) echo 'checked'; ?>
name="categories_id<?php echo $crow['categories_id']; ?>" id="categories_id<?php echo $crow['categories_id']; ?>"><?php echo $crow['categories_name']; ?>
【讨论】:
以上是关于如何使用会话数组检查复选框的主要内容,如果未能解决你的问题,请参考以下文章