我必须使用jquery到PHP来发布选定的输入复选框。我将数组插入到隐藏的输入字段中。当我在PHP中收到数组时,仅删除一个用户ID。但我希望从数据库中删除所有选定的用户ID。
我的jquery代码是:
$("#delSelected").click(function(){
var checks = [];
$.each($("#userList input:checked"), function(){
checks.push($(this).val());
});
$("#modal-user").html("Selected Users");
$("#modal-footer").html("<form action='' method='post'><input type='hidden' name='checkboxes[]' value='"+ checks +"'><button class='btn btn-warning btn-sm' type='submit' name='submit'>Yes Delete</button></form><button type='button' class='btn btn-secondary btn-sm' data-dismiss='modal'>Cancel</button>");
});
我的PHP代码是:
if(isset($_POST['submit'])){
$checkboxes = $_POST['checkboxes'];
foreach($checkboxes as $uId){
mysqli_query($con, "DELETE FROM users WHERE id = '$uId' ");
}
}
答案 0 :(得分:1)
您应该设置数组索引。
if(isset($_POST['submit'])){
$checkboxes = $_POST['checkboxes'];
foreach($checkboxes[0] as $uId){
mysqli_query($con, "DELETE FROM users WHERE id = '$uId' ");
}
}