我的代码是在检查最上部复选框后检查,然后在我克隆删除所有按钮时删除所有选中的复选框
我的问题是我无法删除复选框
我想删除选中的行
这是我的查看页面
jQuery(".checkall").on("click",function(){
var checkall = jQuery(this);
jQuery(".rows input[type='checkbox']").each(function(){
var rows_this = jQuery(this);
var row_id = rows_this.attr("data-id");
var parent = rows_this.closest("tr");
var view_button = parent.find(".view");
var href = "personal_information.php?id=" +row_id;
var delete_button = parent.find(".delete");
var href1 = "delete.php?id=" +row_id;
if(checkall.is(":checked")){
rows_this.prop("checked",true);
view_button.removeClass("color");
view_button.attr("href",href);
delete_button.attr("href",href1);
delete_button.removeClass("gray");
}else{
rows_this.prop("checked",false);
view_button.addClass("color");
view_button.attr("href","javascript:void(0);");
delete_button.attr("href","javascript:void(0);");
delete_button.addClass("gray");
}
});
});
这是我的 jQuery
<table><form>
<tr>
<th><input type="checkbox" class="checkall"></th>
<th style="width:100px;"><center>Last Name</center></th>
<th style="width:100px;"><center>First Name</center></th>
<th style="width:auto;"><center>Email</center></th>
<th style="width:100px;"><center>Birthday</center></th>
<th style="width:auto;"><center>Action</center></th>
</tr>
<tr>
<?php
while($row = mysql_fetch_array($result))
{
echo "<tr class='rows' id='".$row['id']."'>";
echo "<td>";
echo "<center><input type='checkbox' data-id='".$row['id']."'></center>"; // data-id the id per row
echo "<td>".$row['lastname']."</td>";
echo "<td>".$row['firstname']."</td>";
echo "<td>".$row['email']."</td>";
echo "<td>".$row['bdate']."</td>";
echo "<td><center>";
echo "<a href='javascript:void(0);' style='text-decoration:none' class='btn delete gray'>DELETE</a> ";
echo "<a href='javascript:void(0);' style='text-decoration:none' class='btn view color'>VIEW</a> ";
echo "</td></center></td>";
echo "</tr>";
}
?>
</tr>
<tr><td>
<input type="submit" value="Delete" class="deleteall">
</td></tr>
</form></table>
这是我的删除页面php
<?php
include 'dbconfig.php';
$id = $_GET['id'];
$sql = "DELETE FROM user WHERE id =".$id;
mysql_query($sql);
Header("Location: user.php");
?>
答案 0 :(得分:1)
您需要name
属性才能将数据发送到后端:
echo "<center><input type='checkbox' name='id[]' data-id='".$row['id']."'></center>";
// and BTW `<center>` tag is removed in HTML5, so better to look for other way
并在deleteall
按钮上添加名称属性:
<input type="submit" name='deleteall' value="Delete" class="deleteall">
现在您可以在表单上添加一些属性:
<form action='delete.php' method='get'>
并且当您发送已检查值的数组时:
if(isset($_GET['deleteall'])){ // check if form is submitted using deleteall button
$id = $_GET['id']; // get the array
for($i=0;$i<count($id);$i++){ // loop through each ids in the array
$id = $id[$i]; /// get the id in the iteration
$sql = "DELETE FROM user WHERE id=".$id; // make the query
$result = mysqli_query($sql); // execute to delete
}
Header("Location: user.php"); // when all deleted then navigate
}
根据您的评论,您可以使用jQuery将所有选中的复选框发送回服务器:
$('.deleteall').click(function(e){
e.preventDefault(); // <---use this stop the form submission.
var arr2send = $('.rows input[type=checkbox]:checked').map(function(){
return $(this).data('id');
}).get();
var deleteallurl = 'delete.php?id='+JSON.stringify(arr2send);
window.location.href = deleteallurl;
});
然而,您需要在后端使用循环来迭代数组中的所有ID并删除它们,在完成所有删除后,您可以导航回user.php
页面。
答案 1 :(得分:0)
SQL注入警报!!
即使您的代码无效:
$sql = "DELETE FROM user WHERE id =".$id;
是 BAD PRACTICE !!
你要求SQL注射,这是你不想要的!即使是脚本小子也可以放弃你的数据库等。
请检查:https://www.owasp.org/index.php/SQL_Injection,至少为了开发人员的利益!
除此之外:
始终在exit
Header("Location: ...)