简介
我正在使用json,ajax和php进行datatables.net jquery服务器端处理。我可以单击行按钮,然后从数据库中删除该行。但是,该页面永远不会使用ajax.reload();刷新页面。
问题
此代码:
console.log("success function data reached");
似乎永远无法达到。是什么原因造成的?
我没有错误,并且该表正确地从数据库中删除了,这就是我被卡住的原因。 datatables.net上的所有内容似乎都很好,我很可能遇到了php问题?
Index.php
$(document).on('click','.delete_btn',function (){
var id = $(this).attr("id").match(/\d+/)[0];
var del_id = $('#example').DataTable().row( id ).data();
var del_id = del_id[0];
console.log(del_id[0]);
$.ajax({
type:'POST',
url:'delete.php',
dataType: 'json', //This says I'm expecting a response that is json encoded.
cache: false,
data: { 'del_id' : del_id},
success: function(data){
console.log("success function data reached");
if(data=='result_ok'){ //You are checking for true/false
//document.location.reload(true);
alert(data);
table.ajax.reload();
}else{
console.log('The row was not deleted.');
}
}
});
});
delete.php
$del_id = $_POST['del_id'];
$stmt = $conn->prepare("DELETE FROM employees WHERE id = ?");
$stmt->bind_param('i', $del_id);
$confirmDelete = $stmt->execute();
//header("location: index.php");
if($confirmDelete){ //Check to see if there was an affected row.
echo "result_ok";
}
echo json_encode($confirmDelete);
?>