如何使用会话数组

时间:2016-09-15 13:43:14

标签: php jquery arrays session checkbox

我是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,否则为0

请告诉我。

1 个答案:

答案 0 :(得分:0)

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']; ?>