jeudi 15 septembre 2016

How to check the checkbox using session array

I am new to PHP, I've been working on the checkbox for days but still not able to solve it. I was trying to make the checkbox checked by using 0 and 1 as status in session. The problem is it won't show checked after submitted but it is actually checked in the session. In the POST, i use window.location.href to make the page refresh so that the checkbox is shown as checked. If i didn't use window.location.href, just like i mentioned above, checkbox is checked but not showed after clicking on the button for the first time. When i clicked the button again, the checkbox will be shown as checked and the session status is also checked, same goes to other checkbox.

<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>

Here is my POST function

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    }

Please advise me




Aucun commentaire:

Enregistrer un commentaire