我有記錄列表。我想通過單擊它來刪除一條記錄,但是當我使用這個$.ajax()
函數時,它將刪除該行,但不會從數據庫中刪除。無法使用ajax函數刪除數據庫中的行
HTML代碼
<tbody>
<tr class="gradeX odd">
<td class="sorting_1">2</td>
<td class=" ">test</td>
<td class=" ">test</td>
<td class="center ">0</td>
<td class="center ">Active</td>
<td class=" "><button type="button" class="btn btn-default btn-circle" name="editRecord"><i class="fa fa-pencil-square-o"></i></button>
<button type="button" class="btn btn-danger btn-default btn-circle" id="2" name="deleteRecord"><i class="fa fa-times"></i></button></td>
</tr>
<tr class="gradeX even">
<td class="sorting_1">3</td>
<td class=" ">sarees</td>
<td class=" ">contains sarres</td>
<td cl="center ">1</td>
<td class="center ">Active</td>
<td class=" "><button type="button" class="btn btn-default btn-circle" name="editRecord"><i class="fa fa-pencil-square-o"></i></button>
<button type="button" class="btn btn-danger btn-default btn-circle" id="3" name="deleteRecord"><i class="fa fa-times"></i></button></td>
</tr>
</tbody>
jQuery的
$(document.body).on('click', '[name="deleteRecord"]', function(){
var id= $(this).attr('id');
if (confirm("Are you sure you want to delete this row?"))
{
var id = $(this).attr('id');
var data = 'id=' + id ;
var parent = $(this).parent().parent();
$.ajax(
{
type: "POST",
url: "admin_operation.php?mode=delete_category",
data: data,
cache: false,
success: function(data)
{
console.log(data);
parent.fadeOut('slow', function() {$(this).remove();});
}
});
}
});
PHP代碼
$mode = $_GET['mode'];
if($mode=='delete_category')
{
$id=$_POST['id'];
$q=$db->query("DELETE FROM db_category WHERE category_id='".$id."'");
if($q){ echo "yes";}
else{ echo "no";}
}
嘗試'變種數據[ 'ID'] = id;'而不是'var data ='id ='+ id;',數據節點期望鍵值對。您也可以在接收的PHP腳本中使用'print_r($ _ POST);'來檢查收到的POST值。 – SaschaM78
另一個想法:'category_id'具有哪種數據類型?如果它是INTEGER類型,則在其值周圍使用單引號會導致錯誤,而不是從表中刪除該行。 – SaschaM78