简体   繁体   中英

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 ?

If yes how can I do this?

Please forgive me I am still learning.

在此处输入图片说明

This is my code from delete_table.php

PS: this is how the delete button is on the site for example: 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 .

<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.

$(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. The confirm() method returns true if the user clicked "OK", and false otherwise. So Save this result in variable and based on that fire ajax or do nothing.

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

ADD if(response ==1) then remove row from HTML

                    // Removing row from HTML Table

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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