【发布时间】:2020-11-27 23:47:04
【问题描述】:
我正在寻找一种在不刷新页面的情况下通过选中复选框来更新我的 sql 数据库的方法。 当用户选择复选框时,我试图实现要传递给 dolead.php 的 id,而不是检查 id 是否高于 0,它必须传递值 1,否则传递值 0。检查后我需要数据库使用发布的 id 的 where 子句进行更新。在我的代码下面,我做错了什么?我的日志中没有错误,并且错误日志如下所示:mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
用户看到的 PHP 选择开关框来触发选择和 jquery 来触发帖子。
<head>
<script src="jquery-3.2.1.min.js"></script>
<link rel="stylesheet" href="switch.css" type="text/css">
</head>
<body>
<table id="myTable" align='left' class="deftable">
<thead>
<tr>
<th style="width:10px !important;">!!</th>
<th>Name</th>
<th>Notes</th>
<th>Category</th>
</tr>
</thead>
<tbody>
<? //Selecteer accounts.
$stmt = $mysqli->prepare("SELECT * FROM account WHERE purpose = 'Crew4' AND level <= ? AND betaald = 'yes'
AND group_name = ? AND secret = 'no' ");
$stmt->bind_param("is", $status,$groupname);
$stmt->execute();
$result = $stmt->get_result(); //only works when nd_mysli is set on the server!
while ($row = $result->fetch_assoc()) {
?>
<tr>
<td class="footer">
<label class="switch">
<input type="checkbox" <?php if($row['do_lead']=='1') {
echo 'checked="checked"';
} ?> value="<?php echo filter_var($row['id'], FILTER_VALIDATE_INT); ?>" />
<div class="slider round">
<span class="on">ON</span>
<span class="off">OFF</span>
</div>
</label></td>
<td><?= htmlspecialchars($row['name']) ?></td>
<td><?= htmlspecialchars($row['notes']) ?></td>
<td><?= htmlspecialchars($row['purpose']) ?></td>
</tr>
<? } ?>
</table>
</body>
<script>
$(document).ready(function()
{
var x=$("input[type=checkbox]").length;
$('input[type=checkbox]').on('change', function(i)
{
if($(this).is(':checked'))
{
x-=1;
if(x==0)
{
var val = [];
$(':checkbox:checked').each(function(i)
{
val[i] = $(this).val();
});
var jsonString = JSON.stringify(val);
$.ajax(
{
type: "POST",
url: "dolead.php",
data: {checkbox_value:jsonString},
cache: false,
success: function(data)
{
/* alert if post is success */
}
});
}
}
else
{
x+=1;
}
});
});
</script>
</html>
dolead.php 用于检索 post 值并更新数据库;
$data = json_decode(stripslashes($_POST['checkbox_value']));
foreach($data as $d){
if($d > 0){$leadupdate = 1;}
if($d == ''){$leadupdate = 0;}
$stmt48 = $mysqli->prepare("UPDATE account SET do_lead = ? WHERE id = ? ");
$stmt48->bind_param("ii", $leadupdate,$d);
$stmt48->execute();
$stmt48->close();
}
?>
PS:连接文件已包含,但此处未显示..
【问题讨论】:
-
JS 错误怎么办?你检查过开发者工具栏控制台吗?
-
是的。没有显示错误。
-
网络标签呢?您确定 AJAX 调用发生了吗?你会得到什么回应?
-
嗯。当我选择这些框时,我没有看到任何活动..
-
看起来通话没有发生..脚本有什么问题..??