zan
zan

Reputation: 99

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.

enter image description here

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();
  });
});

Upvotes: 0

Views: 7114

Answers (3)

Chand Mehr
Chand Mehr

Reputation: 1

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();
                    });

Upvotes: 0

Gurpreet Singh
Gurpreet Singh

Reputation: 209

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
}

Upvotes: 0

user9139688
user9139688

Reputation:

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>

Upvotes: 1

Related Questions