Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Page is not waiting for response from SweetAlert confirmation window

I am trying to upgrade my JavaScript confirm() action to use SweetAlert. Currently my code is something like this:

<a href="/delete.php?id=100" onClick="return confirm('Are you sure ?');" >Delete</a>

This waits for the user to confirm before navigating to the delete page. I would like to use this example from SweetAlert to ask the user to confirm before deleting:

swal({
 title: "Are you sure?",   
 text: "You will not be able to recover this imaginary file!",   
 type: "warning",   
 showCancelButton: true,   
 confirmButtonColor: "#DD6B55",   
 confirmButtonText: "Yes, delete it!",   
 cancelButtonText: "No, cancel plx!",   
 closeOnConfirm: false,   
 closeOnCancel: false 
}, 
function(isConfirm){   
  if (isConfirm) {     
    swal("Deleted!", "Your imaginary file has been deleted.", "success");
  } 
  else {     
    swal("Cancelled", "Your imaginary file is safe :)", "error");  
  } 
});


Everything I have tried has failed. When the first alert is displayed, the page has gone ahead and deleted the item and refreshed before the user has even clicked on the alert buttons. How do I make the page wait for the users input?

Any help would be greatly appreciated.

like image 344
dmeehan Avatar asked Jan 16 '15 15:01

dmeehan


People also ask

What is SweetAlert?

Sweet Alert is a way to customize alerts in your games and websites. It allows you to change from a standard JavaScript button. We can add buttons, change the color text, and even add additional alerts that change depending on user click.

How do I return a value from SweetAlert?

Option 1: Function confirm need a return value. Update a var and return it at the end. Option 2: Passes the function as parameter to be executed and start it if confirm. confirm("my message", myFunction()); function confirm(message, FunctionByConfirm) { if (isConfirm) { FunctionByConfirm(); } else { // nothing. }

How do you call SweetAlert?

Once the library is set up, creating a SweetAlert message is very easy. All you have to do is call the swal() function. swal("Here's a message!", " Have a nice day!")


2 Answers

You cannot use this as a drop-in replacement for confirm. confirm blocks the single thread of execution until the dialog has been acknowledged, you cannot produce the same behavior with a JavaScript/DOM-based dialog.

You need to issue a request to /delete.php?id=100 in the success callback for your alert box.

Instead of...

swal("Deleted!", "Your imaginary file has been deleted.", "success");

You need

<a href="#">Delete<a>
...

$.post('/delete.php?id=100').then(function () {
  swal("Deleted!", "Your imaginary file has been deleted.", "success");
});

You also must fix your delete.php to only accept POST requests. It's a huge problem to allow GET requests to delete resources. The first time Google or any other crawler finds your page, it will look at the href of every link in your document and follow each link, deleting all of your content. They will not be stopped by the confirm box, as they probably (with the exception of Google) won't be evaluating any JavaScript.

like image 166
meagar Avatar answered Sep 22 '22 00:09

meagar


You can do it this way.

HTML:

<a href="/delete.php?id=100" class="confirmation" >Delete</a>

JS:

$('.confirmation').click(function (e) {
    var href = $(this).attr('href');

    swal({
        title: "Are you sure?",
        type: "warning",
        showCancelButton: true,
        confirmButtonColor: "#DD6B55",
        confirmButtonText: "Yes, delete it!",
        cancelButtonText: "No, cancel plx!",
        closeOnConfirm: true,
        closeOnCancel: true
    },
            function (isConfirm) {
                if (isConfirm) {
                    window.location.href = href;
                }
            });

    return false;
});

Seems a hack but it's working for me.

like image 36
Abdelhakim AKODADI Avatar answered Sep 22 '22 00:09

Abdelhakim AKODADI