0

I have this partially working code. What it suppose to do is to check for existing email address in database. If no email exist then return true;

 $(document).ready(function() {

    var email_check = 0;    

        var checking_html = '<img src="http://i.imgur.com/Y8wXAZp.gif" /> Checking...';
        var characters_error = 'Minimum amount of chars is 6';

        //when button is clicked
        $('.register').click(function(){    

            //run the character number check
            if($('#email').val().length < 1){

                $('#email_result').html(characters_error);
                return false;
            }                   
            else{           
                //else show the cheking_text and run the function to check
                $('#email_result').html(checking_html);

                alert(check_email_availability());
            }
        });


  });

//function to check email availability  
function check_email_availability(){

        //get the email
        var email = $('#email').val();

        //use ajax to run the check
        $.post("check_email.php", { email: email },
            function(result){
                email_check = 0;
                //if the result is 1
                if(result == 1){
                    //show that the email is available
                    email_check = 1;

                }else{
                 email_check = 0;
                }

        });

        if (email_check == 1){  
             return true;   
        }else{
             return false;      
        }   

}  

Now, the problem is if current state is false, when I enter an email that is not available in the db and click button, I still get false alert, and when the next time I click button I get true alert. For some reason the function execute bottom code first (checking email_check value) and after that it execute the function. Why is that? What is wrong with my code? How can I make it execute function and then check the email_check value whether 1 or not?

2
  • 1
    blog.slaks.net/2015-01-04/async-method-patterns Commented Feb 15, 2015 at 1:15
  • 3
    The post request runs asynchronously which means it will evaluate once it gets it's response. Your if block will run before the post has returned. Commented Feb 15, 2015 at 1:18

2 Answers 2

2

I would change this to put an ajax success callback on your check function something along the lines of.

success: function (data, status, xhr ) {
   myFunctionShowEmailSuccess();
},
error: function (xhr, status, error) {
    myFunctionShowEmailFailure();
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for giving me idea. I use almost similar approach. My problem has been solved. Thanks again.
0

Try doing this.

//function to check email availability  
function check_email_availability(){

        //get the email
        var email = $('#email').val();

        //use ajax to run the check
        $.post("check_email.php", { email: email },
            function(result){
                email_check = 0;
                //if the result is 1
                if(result == 1){
                    //show that the email is available
                    return true;
                }else{
                 return false;
                }

        });

}

1 Comment

Unfortunately, I don't get any alert.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.