Note that there are some explanatory texts on larger screens.

plurals
  1. POajax success callback executes out of expected order
    primarykey
    data
    text
    <p>I have a function that validates some fields when a button is pressed. First one checks if the username and password are authentic or not. then if the password is appropriately secure. Then if it matches the confirm-password box. However, it seems that the ajax that check to see if the user is authentic does not complete before the alert in the first function pops up.</p> <pre><code>$(document).ready(function() { $('#submit_pw_ch').on('click', function(){ var alertString = "Please correct the following errors: \n" //current username and password are correct var vUsr = valUsr(); //new password is sufficiently secure var vPwd = valPwd(); //confirmation of new password is same as previous box var sPwd = samePwd(); console.log('valid user : ' + vUsr + ' valid password : ' + vPwd + ' same password : ' + sPwd); //append appropriate warnings to alert string if ( !vUsr ) { alertString += "-Your current username and password are invalid. \n"; } if ( !vPwd ) { alertString += "-The new password you have selected is not strong enough. \n"; } if ( !sPwd ) { alertString += "-The confirmation of your new password does not match the previous entry. \n"; } if ( !vUsr || !vPwd || !sPwd ) { alert(alertString); return false; } else { //change password } }); }); </code></pre> <p>So the line that checks for that is <code>var vUsr = valUsr();</code> which calls</p> <pre><code>function valUsr() { var un = $('#uNameInput').val(); var pw = $('#uPwdInput').val(); //return value var validUsr = false; $.ajax({ type: "post", url: "queries/confirm_user.php?&lt;?=time()?&gt;", data: "un=" + un + "&amp;pw=" + pw, dataType: "json", success: function (returnedData) { console.log(returnedData) if (data == 'true') { validUsr = true; } else { validUsr = false; } } }); return validUsr; } </code></pre> <p>Somehow though the alert is not waiting for the ajax to finish getting it's data. The <code>console.log(returnedData)</code> in the <code>valUsr()</code> function appears in the console after I've dismissed the alert box. Why is this happening? How can I prevent it? Thanks!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload