我如何可以动态添加复选框数组和索引索引的复选框,并在jQuery中使用

时间:2019-03-31 18:26:10

标签: php jquery arrays ajax checkbox

我有此代码,我想根据从数据库中获取的编号添加复选框,并且每个复选框在另一个表中都有一个专有行 因此,当我在浏览器中显示此代码时,浏览器为所有复选框设置了状态复选框1(每个复选框的状态分别为2和0和1),因此我尝试为获取的复选框创建数组,并且必须将复选框更改发送给php代码, jQuery并保存在db中,但是我无法编写有用的代码来解决此问题,任何机构都可以帮助我或编辑我的代码?

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

0 个答案:

没有答案