我有此代码,我想根据从数据库中获取的编号添加复选框,并且每个复选框在另一个表中都有一个专有行 因此,当我在浏览器中显示此代码时,浏览器为所有复选框设置了状态复选框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>