我有一个表格,用户可以删除每条记录:
<td style='text-align: center;'>
<button class='btn btn-danger btn-xs delete' id=9 data-title='Delete' data-toggle='modal' data-target='#delete' >
<span class='glyphicon glyphicon-trash'></span>
</button>
</td>
jQuery
/ AJAX
来电是:
$('.delete').click(function(e) {
var id = $(this).attr('id');
e.preventDefault();
alert(id);
bootbox.confirm("Are you sure? ", function(r) {
if (r) { // Sent request to delete order with given id
alert('SENT REQUEST');
$.ajax({
type: 'GET',
url: 'delete.php',
data: 'id='+id,
success: function(b) {
if (b) { // Delete row
alert('YES');
// orTable.fnDeleteRow($('tr#' + id)[0]);
} else { // Failed to delete
alert('NO');
noty({
text: "There is a problem deleting this record, try again",
layout: "topCenter",
type: "alert",
timeout: 3
});
}
}
});
document.location.reload();
}
});
});
delete.php是
if($_GET['id']) {
$id = $_GET['id'];
$sql_delete = "DELETE FROM debiteuren WHERE id = " . $id;
mysqli_query($link,$sql_delete) or die(mysqli_error($link));
}
知道为什么它不起作用吗?
我收到提醒ID和模式“你确定吗?”和警报“发送请求”。但之后,页面重新加载和ID = 9的记录尚未删除。
亲切的问候,
Arie
答案 0 :(得分:2)
你的ajax执行异步尝试在ajax完成后重新加载页面:
$('.delete').click(function(e) {
var id = $(this).attr('id');
e.preventDefault();
alert(id);
bootbox.confirm("Are you sure? ", function(r) {
if (r) { // Sent request to delete order with given id
alert('SENT REQUEST');
$.ajax({
type: 'GET',
url: 'delete.php',
data: {id:id},
success: function(b) {
if (b) { // Delete row
alert('YES');
// orTable.fnDeleteRow($('tr#' + id)[0]);
} else { // Failed to delete
alert('NO');
noty({
text: "There is a problem deleting this record, try again",
layout: "topCenter",
type: "alert",
timeout: 3
});
}
document.location.reload();
}
});
}
});
正如Rayon Dabre建议从php页面返回一些东西
注意使用data-id="9"
获取有效的html
答案 1 :(得分:1)
我觉得你reload()
在错误的地方,应该在success
回调中移动。
success: function(b) {
if (b) { // Delete row
alert('YES');
// orTable.fnDeleteRow($('tr#' + id)[0]);
} else { // Failed to delete
alert('NO');
noty({
text: "There is a problem deleting this record, try again",
layout: "topCenter",
type: "alert",
timeout: 3
});
}
document.location.reload(); //<-----move it here.
}
我想你每次都会得到NO
的警报,因为你没有从服务器发回适当的回复。