gpt4 book ai didi

javascript - 服务器端表使用 JS、Php 或 Ajax 从表(但不是数据库)中删除行

转载 作者:行者123 更新时间:2023-11-29 23:18:02 25 4
gpt4 key购买 nike

项目链接:https://databasetable-net.000webhostapp.com/

以下代码正确地删除了表格中的一行:

  $('#example').on('click', '.delete_btn', function () {
var row = $(this).closest('tr');
var data = table.row( row ).data().delete;
console.log(data);
alert("delete_btn clicked");
row.remove();
});

但是,它并没有永久删除该行。如果刷新页面,被“删除”的行仍然存在。我相信这是因为我没有从数据库中删除该行。通常在 php 中,你可以安全地删除数据库中的一行,如下所示:

id = mysqli_real_escape_string($con, $_GET['del']);
$stmt = $con->prepare("DELETE FROM employees WHERE id = ? LIMIT 1");
$stmt->bind_param('i', $id);
$stmt->execute();
$stmt->close();
header('location: index.php');

编辑:修订代码 Index.php:

  (document).ready(function() {
var asc = true;
var table = $('#example').DataTable( {
"processing": true,
"serverSide": true,
"ajax": {
"url": "server.php",
"type": "POST",
},

//http://live.datatables.net/xijecupo/1/edit
columnDefs: [{
targets: -1,
defaultContent: '<button type="button" class="delete_btn">Delete</button>'
}],
rowGroup: {
dataSrc: 1
}
});

$(function(){
$(document).on('click','.delete_btn',function(){

var del_id= $(this).closest('tr');
var ele = $(this).parent().parent(); //removed the "$" from the ele variable. It's a js variable.
console.log(del_id);

$.ajax({
type:'POST',
url:'delete.php',
dataType: 'json', //This says I'm expecting a response that is json encoded.
data: { 'del_id' : del_id},

success: function(data){ //data is an json encoded array.

console.log('Data: ' + data); //Going to display whats in data so you can see whats going on.

if(data['success']){ //You are checking for true/false not yes or no.
console.log('You successfully deleted the row.');
alert("delete btn clicked");
ele.remove();
}else{
console.log('The row was not deleted.');
}

}

});
});
}); //http://jsfiddle.net/zfohLL0a/

}); //end doc ready

删除.php代码:

$del_id = $_POST['del_id']; 
$stmt = $conn->prepare("DELETE FROM employees WHERE id = ?"); //LIMIT 1
$stmt->bind_param('i', $del_id);
$confirmDelete = $stmt->execute();

$array['success'] = FALSE; //Initialize the success parameter as false.
if($confirmDelete){ //Check to see if there was an affected row.
$array['success'] = TRUE;
}

echo json_encode($array);
?>

部分解决方案: Sample format how to setup the ajax. 您必须首先使用 datatables.net“ajax”:原始 server.php 的方法。但是在那之后,您对 add.php、delete.php 等使用普通的 $.ajax 方法。这很困惑,因为您对 ajax 使用了两种不同的语法。最简单的方法是查看示例链接。 Youtube video for same code

另一个讨论向 ajax/json 发送信息和从 ajax/json 发送信息的有用链接是 one two three Four

最佳答案

使用您最新更新的代码更新答案。

JS

$(function(){
$(document).on('click','.delete_btn',function(){

var del_id= $(this).closest('tr');
var ele = $(this).parent().parent(); //removed the "$" from the ele variable. It's a js variable.
console.log(del_id);

$.ajax({
type:'POST',
url:'delete.php',
dataType: 'json', //This says I'm expecting a response that is json encoded.
data: { //Set up your post data as an array of key value pairs.

'del_id' : del_id

},

success: function(data){ //data is an json encoded array.

console.log('Data: ' + data); //Going to display whats in data so you can see whats going on.

if(data['success']){ //You are checking for true/false not yes or no.
console.log('You successfully deleted the row.');
ele.fadeOut().remove();
}else{
console.log('The row was not deleted.');
}

}

});
});
});

删除.php

$del_id = $_POST['del_id']; 
$stmt = $con->prepare("DELETE FROM employees WHERE id = ?"); //LIMIT 1
$stmt->bind_param('i', $del_id);
$confirmDelete = $stmt->execute();

$array['success'] = FALSE; //Initialize the success parameter as false.
if($confirmDelete){ //Check to see if there was an affected row.
$array['success'] = TRUE;
}

echo json_encode($array); //Your ajax is setup to expect a json response.
//json_encode the $array and echo it out. You have to do this.
//When you "echo" out a value, that is what the server is going to submit back to the ajax function.
//If you do not do this, the ajax script will not recieve a response from the delete.php page.

此代码应该适合您。

关于javascript - 服务器端表使用 JS、Php 或 Ajax 从表(但不是数据库)中删除行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51923576/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com