简介
我正在使用json,ajax和php进行datatables.net jquery服务器端处理。我可以单击行按钮,然后从数据库中删除该行。但是,该页面永远不会使用ajax.reload();
刷新页面。项目链接 https://databasetable-net.000webhostapp.com/
问题
此代码:
console.log("success function data reached");
似乎永远无法达到。是什么原因造成的?
我没有错误,并且该表正确地从数据库中删除了,这就是我被卡住的原因。 datatables.net上的所有内容似乎都很好,我很可能遇到了php问题?
Index.php
<script type="text/javascript">
$(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.');
}
}
});
});
</script>
delete.php
<?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);
?>
答案 0 :(得分:0)
DataTable dt = new DataTable();
foreach (DataRow dr in dt.Rows)
{
if (dr["Column_Name"] == DBNull.Value)
{
//Do something
}
else
{
//Do something
}
}
并且php代码必须为:
<script type="text/javascript">
$(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',
data: {del_id:del_id},
success: function(data){
if (data=='DEL_OK') {
location.reload();
} else {
alert('something wrong')
}
})
});
</script>
答案 1 :(得分:0)
根据我对您的问题的了解,您实际上没有正确收到php
中ajax
的答复。 ajax响应中的data
实际上是一个字符数组。
$.ajax({
type:'POST',
url:'delete.php',
data: {del_id:del_id},
success: function(data){
if (data[0]=='D') {//changing here
location.reload();
} else {
alert('something wrong')
}
})
});
在php中
<?php
$del_id = $_POST['del_id'];
$stmt = $conn->prepare("DELETE FROM employees WHERE id = ?");
$stmt->bind_param('i', $del_id);
$confirmDelete = $stmt->execute();
if($confirmDelete) {
echo "D";// echoing only D
}
?>