i've been searching and searching for the best way to do this and can't come up wit ha definitive answer
I'm validating a form by posting the values to a php script which checks if they exist in the database already. I have several fields that i need to validate, one of them beign email.
I have the following rudimentary code in place
jQuery(document).ready(function(){
jQuery("#signup_submit").click(function(){
var u_email = jQuery('#signup_email').val();
var email_check = 0;
var email_checked = "no";
if (jQuery('#signup_email').val()) {
jQuery.post("/check-username-email",{ user_email:jQuery('#signup_email').val() } ,function(data){
//if email not avaiable
if(data == 0) {
email_checked = "yes";
email_check = 0;
alert(email_check + " 1");
}
//if email is not vaild
else if(data == 2){
email_checked = "yes";
email_check = 0;
alert(email_check+ " 2");
}else{
email_checked = "yes";
email_check = 1;
alert(email_check + " 3");
}
});
}else{
email_checked = "yes";
email_check = 0;
alert(email_check + " 4");
}
// IN NEED THIS NEXT BIT TOW WAIT UNTIL THE PREVIOUS SECTION HAS FINISHED BEFORE EXECUTING
if (email_checked=="yes"){
if (email_check==1){
alert("ok");
}else{
alert("notok");
}
}else{
alert("email not checked");
}
//jQuery("#signup_form_standalone").submit()
});
});
My problem is that the if statement at the bottom is executing before the first function has validated.
I've read up about jQuery.when and callbacks(), tried them and can't get it to work as needed.
What am i doing wrong? There has to be a simple way to achieve what i'm tryign to do, but i have no idea what it is :) I've used queue and callbacks in the past when dealign with animations but these don't seem to apply in the same way here.
Cheers.