我想刪除動態創建的記錄,從一個動態創建的形式,刪除記錄,但代碼不working..Please看看使用AJAX和PHP
的script.js
$(function() {
$('#studentRecord').on('click', 'button.delete', function() {
var $tr = $(this).closest('tr');
var $id = parseInt($(this).attr('id').split('-')[1]);
if (confirm("Are you sure you want to delete this record")) {
$.ajax({
url: '/ab_batch/practice/db/action.php',
type: 'POST',
data: {
action: 'ajaxDelRecord',
id: JSON.stringify(id)
},
success: function() {},
error: function() {}
});
}
});
});
action.php的
$action = (isset($_REQUEST['action']) && !empty($_REQUEST['action'])) ? $_REQUEST['action'] : null;
switch ($action) {
case 'ajaxDelRecord':
if (isset($_POST['id'])) {
$id = json_decode($_POST['id']);
}
$id = json_decode($_POST['id']);
print ($student->delRecord($id)) ? 'true' : 'false' ;
break;
}
有一個包含刪除所有API另一個db.php文件,添加和更新
''JSON.stringify(id)'應該是'JSON.stringify($ id)',因爲您將'id'聲明爲'$ id',您並不需要:'JSON.stringify'只是發送一個整數到服務器。 –
@ArshSingh非常感謝它的工作,但頁面需要重新加載才能查看結果...因爲我使用ajax不應該結果反映一旦我點擊刪除按鈕? –
您應該在服務器回答您的ajax請求後刪除div。它不會反映變化,直到你沒有說要通過發送請求到服務器刪除你最近刪除的特定html元素 –