简体   繁体   English

使用 jQuery AJAX 删除记录之前的确认警报不起作用

[英]Confirmation alert Before Delete record with jQuery AJAX not working

My problem is that when I press yes on delete confirmation the db record goes away,but if I refresh the page it comes back.我的问题是,当我在删除确认时按是,数据库记录消失了,但如果我刷新页面,它又回来了。

Maybe I have to put the language and the id inside the Ajax URL ?也许我必须将语言和 id 放在 Ajax URL 中?

If yes how can I do this?如果是,我该怎么做?

Please forgive me I am still learning.请原谅我还在学习中。

在此处输入图片说明

This is my code from delete_table.php这是我来自delete_table.php 的代码

PS: this is how the delete button is on the site for example: delete_table.php?lang=en&id=149 PS:网站上的删除按钮是这样的,例如: delete_table.php?lang=en&id=149

       if (isset($_GET['id']) && is_numeric($_GET['id']))
      {

       $id = $_GET['id'];


      if ($stmt = $conn->prepare("DELETE FROM `articles_".LANG."` WHERE id = 
      ? LIMIT 1"))
      {
     $stmt->bind_param("i",$id);
     $stmt->execute();
     $stmt->close();
      }
      else
      {
    echo "ERROR: could not prepare SQL statement.";
      }

This is the delete button from the index.php .这是index.php 中的删除按钮。

<td><a class='delete' id='del_".$row->id."' href='delete_table.php?lang=".LANG."&id=" . $row->id . "'>Delete</a></td>

This is the delete.js where the jquery and ajax is.这是 jquery 和 ajax 所在的delete.js

$(document).ready(function() {
// Delete 
$('.delete').click(function(event) {
    var el = this;
    var id = this.id;
    var splitid = id.split("_");

    // Delete id
    var deleteid = splitid[1];

    // Confirm box
    bootbox.confirm("Are you sure want to delete this article?",
        function(result) {

            if (result) {
                // AJAX Request
                $.ajax({
                    url: 'delete_table.php',
                    type: 'POST',
                    data: { id: deleteid },
                    success: function(response) {

                        // Removing row from HTML Table
                        $(el).closest('tr').css('background', 'tomato');
                        $(el).closest('tr').fadeOut(800, function() {
                            $(this).remove();
                        });

                    }
                });
            }
            console.log('delete_table.php');
        });
    event.preventDefault();
  });
});

this is what you are looking for这就是你要找的

 function confirmDelete() { if (confirm("Delete Record?") == true) { alert("Now deleting"); return true; } else { alert("Cancelled by user"); return false; } }
 <td><a class='delete' id='del_".$row->id."' href='delete_table.php?lang=".LANG."&id=" . $row->id . "' onclick="return confirmDelete()">Delete</a></td>

Confirm method does not accept function as second argument. Confirm 方法不接受函数作为第二个参数。 The confirm() method returns true if the user clicked "OK", and false otherwise.如果用户单击“确定”,则 confirm() 方法返回 true,否则返回 false。 So Save this result in variable and based on that fire ajax or do nothing.因此,将此结果保存在变量中并基于该火 ajax 或什么都不做。

var result = confirm('Are you sure?');
if(result) {
    // fire ajax
}

ADD if(response ==1) then remove row from HTML添加 if(response ==1) 然后从 HTML 中删除行

                    // Removing row from HTML Table

                    $(el).closest('tr').css('background', 'tomato');
                    $(el).closest('tr').fadeOut(800, function() {
                        $(this).remove();
                    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM