Reload page after click OK on the Alert Page

Viewed 461

I want to reload page after clicking OK button on the javascript Alert box. Here is my code :

$(".erase").click(function () {
    var answer = confirm("Delete This Data?");
    if (answer === true) {
        var erase = false;
        if (!erase) { 
            erase = true; 
            $.post('delete.php', {id: $(this).attr('data-id')} );
            erase = false;  
        }
        window.location.reload();
    } else {
        return false;
    } 
});

if I put the window.location.reload(); there, the page reloading after click OK, but I can't delete the data I want. If I remove it, I can delete the data but the page doesn't reload.

Please help me on this

2 Answers

You just need to provide the window.reload() as a callback to $.post.

$(".erase").click(function () {
    var answer = confirm("Delete This Data?");
    if (answer === true) {
        var erase = false;
        if (!erase) { 
            erase = true; 
            $.post('delete.php', {id: $(this).attr('data-id')}, function() { // here's the new bit
               window.location.reload();
            } );
            erase = false;  
        }
    } else {
        return false;
    } 
});

Change your first line to (I write code from head):

$(".erase").click(async function () {

and line with $.post to this:

let postResult = await Promise.resolve($.post('delete.php', {id: $(this).attr('data-id')} ));
Related