0

I have a Signup page where after the form's submit I send a AJAX POST. There is no problem at all except that, in the success function, I have an alert function that doesn't wait for the user input, but executes the next function immediately.

This is the SubmitHandler Function:

submitHandler: function (form) {
$("#Sign_Button").attr("disabled", "disabled");
    $.ajax({
        type: "POST",
        url: "ws/users/insert.php",
        data: $("#form_sign").serialize(),
        success: function (data) {
            $("#Sign_Button").removeAttr("disabled");
            console.log(data);
            if (data.success == 1) {
                alert("Success.");
                window.location.href='./index.php';
            }
        }
    });
}

Note: I tried with window.location.href and window.location, but in both cases it does the same thing: Popup the alert but also redirect to index.php without waiting, closing the popup alert.

NOTE: Please note that both with Alert and Confirm I have the same behaviour

JuanP. Zuniga
  • 65
  • 3
  • 13

4 Answers4

1

As was answer in this question, you can pause the code using the alert inside of an if. This will also show only an "OK" button (instead of confirm's "yes/no"). It's important to put the ! before the alert call, as the alert function will return always undefined

this is the part of the code with the alert pausing the code:

if (data.success == 1) {
    if(!alert("Success."))
        window.location.href='./index.php';
JuanP. Zuniga
  • 65
  • 3
  • 13
0

I guess you want to use the confirm dialog instead of alert.

Example usage:

if (window.confirm("Do you really want to leave?")) { 
  window.open("exit.html", "Thanks for Visiting!");
}

More info here

ztadic91
  • 2,774
  • 1
  • 15
  • 21
0

This is the exact behaviour that is expected. The alert dialog should be used for messages which do not require any response on the part of the user, other than the acknowledgement of the message.

Dialog boxes are modal windows - they prevent the user from accessing the rest of the program's interface until the dialog box is closed.

Sibtain
  • 1
  • 1
  • 1
0

You would need to put the alert on the ./index.php page

Or you can use the dialog element

(function() {
  var showBtn = document.getElementById('showBtn');
  var myDialog = document.getElementById('myDialog');

  showBtn.addEventListener('click', function() {
    myDialog.showModal();
  });
})();
<dialog id="myDialog">
  <form method="dialog">
    <div>Success</div>
    <button>Ok</button>
  </form>
</dialog>
<button id="showBtn">Show</button>
Matt
  • 2,096
  • 14
  • 20