dimanche 31 mars 2019

how i can add dynamicly checkbox with checkbox array and difrent index and use in jquery

i have this codes and i want to add checkbox according to fetched number from database and each checkbox has a Proprietary row in another table so when i show this code in browser,browser set status checkbox 1 for all checkbox(each checkbox has a 2 status 0 and 1) so i tried to make a array for my fetched checkbox and i have to send checkbox changes to php code with jquery and save in db but i cannt write a useful code for solve this problem any body can help me or edit my code?

$conn = mysqli_connect('localhost', 'root', '', 'win');
$query = "SELECT * FROM users";
$result = mysqli_query($conn, $query);
$row = mysqli_fetch_assoc($result);
$lights = $row['lights'];
$sql1 = "SELECT * FROM `lights` WHERE id='1'";
$result1 = mysqli_query($conn, $sql1);
$row1 = mysqli_fetch_assoc($result1);
$bord_number = $row1['bord_number'];
$i = 0;
$checkbox[0] = "<label class=\"switch\">
            <input type=\"checkbox\">
            <span class=\"slider round\"></span>
            </label></br>";
$checkbox[1] = "<label class=\"switch\">
            <input type=\"checkbox\" checked=\"checked\">
            <span class=\"slider round\"></span>
            </label></br>";

?>
<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" href="mysty.css">
 <script src="../js/ajax.js"></script>
    <script>
        $(document).ready(function () {
            $('#mycheckbox').change(function () {
                var returnVal = ("Are " + "you sure?");
                if (returnVal) {
                    postToServer($(this).prop("checked"));

                } else {
                    $(this).prop("checked", !$(this).is(":checked"));
                }
            });

            function postToServer(state) {
                let value = (state) ? 1 : 0;
                //alert('Posted Value: ' + value);
                $.ajax({
                    type: 'POST',
                    url: 'checkbox.php',
                    data: {'value': +value},
                    success: function (response) {
                        //handle response
                    }
                });
            }
        });
</head>
<body>
<div>
    <?php
    while ($row1 = mysqli_fetch_assoc($result1)) {
        $status = $row1['status'];
        $bord_number = $row1['bord_number'];


        while ($i < $lights) {

            if ($status < '1') {
                echo $checkbox[0];

            } else {
                echo $checkbox[1];

            }
            $i++;

        }
    }

    ?>
</div>
</body>
</html>




Aucun commentaire:

Enregistrer un commentaire