jeudi 26 janvier 2017

Set Checkbox to CHECKED on Page Refresh based off of PHP Conditional

Hopefully this isn't a duplicate, so please bear with me.

I have a form that is a series of checkboxes that sets a boolean value from 0 to 1 in a database, then on Submit, it takes those values, updates the database, then refreshes the page, so that if the value is set to 0 the checkbox is unchecked or if the value is 1, then the checkbox is checked. ALL OF THIS IS WORKING - THERE IS NO ISSUE WITH THIS PART OF MY CODE.

My issue is that above my SUBMIT button, I have a Select/Deselect All button that if ALL are selected, on page refresh, that the checkbox stays checked. But on refresh, the button is unchecked, and I have to click it twice to have the checkbox unselect all of the previously mentioned checkboxes.

Here is my code:

<form action="" method="post">
<?php
    while( $row = mysql_fetch_array($result) ){
        $id = $row['id'];
        $active = $row['active'];
        $name = $row['name'];
        $class = $row['class'];

        if($active == '1'){
            **IF ANY OF THESE CHECKBOXES ARE ACTIVE, THEN THE SELECT/DESELECT ALL BUTTON SHOULD BE CHECKED -- WHAT IS THE PROPER SYNTAX???**
        }


        echo "<input type='hidden' name='activate_".$id."' value='0'>";
        echo "<label class='switch'><input type='checkbox' class='checkbox' name='activate_".$id."' value='1'";

        if($active == '1'){
            echo "checked='checked'";

        }

        echo "/><div class='slider'></div><span>" . $name . "</span></label>";

    }

mysql_close();
?>
<label for="select_all" class="selectAllLabel"><input type="checkbox" id="select_all"/> Activate/Deactivate All</label>
<input type="submit" class="submit" name="submit" value="Update Active Roster">
</form>
 <iframe src="index.html" width="376" height="260" style="border: 0;"></iframe>

<?php

    if(isset($_POST['submit'])){

        require "config.php";

        $conn = new mysqli($db_host, $db_user, $db_pass, $db_name);
        if ($conn->connect_error) {
            die("Connection failed: " . $conn->connect_error);
        } 

        for ($i = 1; $i<count($_POST); $i++){
            $sql = "UPDATE roster SET active = ".$_POST["activate_$i"]." WHERE id = ".$i;
            if ($conn->query($sql) === TRUE) {
                echo "";
            } else {
                echo $conn->error;
            }
        }

        $conn->close();
        echo "<meta http-equiv='refresh' content='0'>";

    }

?>
<script>
$(document).ready(function(){
    //select all checkboxes
    $("#select_all").change(function(){  //"select all" change 
        var status = this.checked; // "select all" checked status
        $('.checkbox').each(function(){ //iterate all listed checkbox items
            this.checked = status; //change ".checkbox" checked status
        });
    });

    $('.checkbox').change(function(){ //".checkbox" change 
        //uncheck "select all", if one of the listed checkbox item is unchecked
        if(this.checked == false){ //if this item is unchecked
            $("#select_all")[0].checked = false; //change "select all" checked status to false
        }

        //check "select all" if all checkbox items are checked
        if ($('.checkbox:checked').length == $('.checkbox').length ){ 
            $("#select_all")[0].checked = true; //change "select all" checked status to true
        }
    });
});
</script>

You can see from my code EXACTLY where I would like the logic to trigger the Select/Deselect All value to be changed. I just don't know the proper syntax to do so.

Thank you in advance.




Aucun commentaire:

Enregistrer un commentaire