$(document).ready(function(){
//global vars
var name = $("#username");
var email = $("#email");
function usernameExists() {
$.get("register.php",{ check: 1, username: name.val(), email: email.val() } ,function(m) {
if(m==1) {
return false;
} else {
return true;
}
});
}
});
Firebug shows the right response when this function is being called, however it returns nothing…(this $.get(…) function has been tested outside the function usernameExists() but without the returns and it worked perfectly).
What’s the problem and how to solve?
$(document).ready(function(){
//global vars
var form = $("#register");
var name = $("#username");
var email = $("#email");
$.get("register.php",
{ check: 1, username: name.val(), email: email.val() },
// Have this callback take care of the rest of the submit()
function(m) {
if(m==1) {
form.submit(function(){ return false; });
} else {
form.submit(function(){
if(validateName() & validateEmail() & validatePass1() & validatePass2())
return true
else
return false;
});
}
}
);
function validateName(){
// some check here
}
// and other functions
});
The function you’re calling doesn’t return anything.
Even if it did try to return the response from your
$.get(), it wouldn’t work because the call is asynchronous, so by the time the response has been received, whatever code that would have used the return value has likely already executed.What you need to do is call your code from within the
$.get()callback.Updated based on your comment.
Probably better just to bring the
$.get()into thesubmit(), but keeping true to your original idea, this is how it could look.Explanation of the joys of synchronous vs. asynchronous javascript
Javascript code normally executes synchronously. That just means that it executes one line at a time, or one line must finish executing before the next line can fire.
This makes things very nice and predictable. But there are some exceptions to that rule. One notable exception is AJAX calls.
Your
$.get()is an example of an AJAX call. The “A” in AJAX stands for asynchronous, which means that it does not prevent the next line of code from executing.The ramification is that when you do a
$.get()that takes (for example) 1 second to complete, whatever code came after the$.get()has long since finished by the time the$.get()has received its response.Take the previous
greetingexample, but this time using AJAX.As you can see, the
alert( greeting )would have executed long before the$.get()response was received, because he$.get()is asynchronous, and doesn’t pause the execution chain while it is waiting for its data.To resolve this, you would place the
alert()inside the callback for$.get(), so that it won’t run until the response is received.The upshot is that in your code, once you call
$.get(), any remaining code that relies on the response received should take place inside the callback.The only way to place your code outside the callback would be to place it in its own function that gets called from inside the callback (like I did with my original answer).
Basic layout of how your code should operate:
Keep in mind, that you don’t necessarily need a separate function for
usernameExists(). You could place all that code inside thesubmit()http://api.jquery.com/jquery.post/