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

标签 javascript php ajax datatable

项目链接: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/

相关文章:

javascript - Jest/expect "toEquals"因未定义的属性而失败

javascript - 如何添加多于document.getElementById?

javascript - 使用 JavaScript 将数据发布到外部 API

javascript - 网页设计师与前端开发人员

javascript - 以下 AngularJS 代码更改底部边框颜色的正确语法是什么?

php - yii向第三张表插入数据

php - 无法使用 php 更改 mysql 中单行的两件事

javascript - 微信分享按钮

javascript - 类型错误 : Cannot read property 'setState' of undefined

javascript - Jquery Ajax IE后访问getElementsByName数组