Is curing done before confirmation?

I decided to change and slightly modify my code using Alertify. My old code is:

<script type="text/javascript"> $('.ask').click(function(){ var answer = confirm('<?php echo $this->translate('Delete route?'); ?>'); return answer; }); 

When I click ok, the php script starts and removes the entry. Javascript returns true or false.

The problem with Alertify is that I can’t even click ok or cancel, my php script works before that. Here is the code:

 $(".ask").click(function(event){ alertify.confirm("Are you sure you want to commit your reservation?", function (e) { if (e) { return true; } else { return false; } }); 

});

I saw some people with similar problems, and I saw that I need to use the default warnings (this makes sense) Code:

 $(".ask").click(function(event){ event.preventDefault(); // cancel submit alertify.confirm("Are you sure you want to commit your reservation?", function (e) { if (e) { return true; // submit form skipping jQuery bound handler } else { } }); 

});

In this case, the PHP script does not start before I click ok / cancel, but it does not start when I click one of the buttons. Nothing happened. Any help please?

+5
source share
1 answer

You are using the asynchronous nature of JavaScript. In the source code, all JavaScript is paused and the confirm dialog is open, so you can directly use the return value from it. However, when you use alertify.confirm , it does not pause the script, so the click handler returns immediately, and any code around it will also be launched, that is, the code to call your PHP script. When you try to execute return true; inside the alertify.confirm callback function, you return from this internal function to alertify code without returning from the external click handler function.

What you need to do is start working with the available callback methods, and not against it - I would suggest rebuilding your code as follows. After confirming that .ask is an a tag, the following should work:

 $(".ask").click(function(event){ var loc = $(this).attr('href'); alertify.confirm("Are you sure you want to commit your reservation?", function (e) { if (e) { document.location.href = loc; } }); //prevent link from being followed immediately on click return false; }); 
+5
source

Source: https://habr.com/ru/post/1202861/


All Articles