0

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.

user1917727
  • 43
  • 2
  • 6
  • Seems like the usual failure to understand how asynchronous (the first "A" in AJAX) code works. See this: [possible duplicate of How to return the response from an AJAX call?](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call) – user229044 Sep 16 '13 at 02:10

1 Answers1

0

Have you thought about just using the HTML attribute required="required" or simple required to handle this. Of course you can further secure this by defining patterns and messages as well. It works very nicely in most browsers and it's worth a shot to avoid a bunch of nasty and buggy javascript sometimes. Just make absolutely sure your PHP server side makes sure the code is perfect and what happens client side is client sides business.

For example I like this for my email inputs upon registration.

<label title="Must be a valid email address for account activation. It will remain private and will not be revealed to other users." for="email">Email:</label>
<input required="required" pattern="^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$" type="text" id="email" name="email" value="" placeholder="Email Address">

This will kick back a large majority of issues and clue the user that they missed something or may have misspelled something. Then just use your favorite PHP function to further validate the email address.

rfoo
  • 1,160
  • 1
  • 12
  • 27
  • He's going to have to learn how to write proper asynchronous JavaScript at some point. If you have "nasty buggy JavaScript", you should work on *fixing* it, not avoid writing it... – user229044 Sep 16 '13 at 02:12
  • Yeah I agree but there comes a point in every programmers life where they realize that you really don't have to do all of this extra stuff when new ideas paving the way are doing away with it. AJAX is wonderful but is it really the most appropriate option for what I believe is a registration form. – rfoo Sep 16 '13 at 02:20
  • Thanks guys, but the whole point is that i need to check if the email is already exiting in the database. So i need to use php/ajax to check the database at some point. I already execute these checks on.blur but this is a final check before submit. – user1917727 Sep 16 '13 at 02:34