One JS file is making an ajax call. Within the success of this ajax call another AJAX call is made. The second call checks whether the email has already been registered. If it has been then the second AJAX call recieves no returned data as shown in firebug and Chrome console in the case. But the same code works just fine in localhost while the mentioned problem takes place ONLY in online server.
The hosted page is at http://twmobilefitness.com/signup/
. The problem takes place when you click on the ‘Register Now’ link at the end. You have to register twice with the same email address if you want to get the problem happen.
JS:
$.ajax( {
url : base_url+"/wp-admin/admin-ajax.php",
type : 'GET',
cache : false,
data : 'action=check_user_name&'
+ Math.floor( ( Math.random() * 100 ) +1 )
+ '&user_name='+user_name,
success : function( result ) {
if ( parseInt( result ) == 0 ) {
$( ".result" ).html( '<span class="error">User name not available </span>' );
} else if ( parseInt( result ) == 1 ) {
$.ajax( {
url : base_url
+ "/wp-admin/admin-ajax.php",
type : 'GET',
cache : false,
data : 'action=check_email_used&'
+ Math.floor( ( Math.random() *100 ) +1 )
+ '&email=' + email,
success : function( result_email ) {
if ( parseInt( result_email ) == 0 ) {
$( ".result" ).html( '<span class="error">Email already used </span>' );
} else if ( parseInt( result_email ) == 1 ) {
$( ".result" ).html( '' );
$( ".signup_div" ).hide();
$( ".signup_emergency_contact" ).show();
}
}
} );
}
}
} );
functions.php
has
add_action('wp_ajax_check_user_name','check_user_name');
add_action('wp_ajax_nopriv_check_user_name','check_user_name');
add_action( 'wp_ajax_check_email_used','check_email_used' );
add_action( 'wp_ajax_nopriv_check_email_used','check_email_used' );
function check_user_name() {
global $wpdb;
$user_name = trim( $_GET['user_name'] );
$MobTraining = new MobTraining();
$table =trim( "{$wpdb->prefix}users" );
$array_where['user_login'] = $user_name;
$sql_fetch = $MobTraining->fetch( $table, $array_where );
$row = $wpdb->get_results( $sql_fetch, ARRAY_A );
if ( sizeof( $row ) != 0 ) {
echo '0';
} else {
echo '1';
}
die();
}
function check_email_used() {
global $wpdb;
$email = trim( $_GET['email'] );
$MobTraining = new MobTraining();
$table = trim( "{$wpdb->prefix}users" );
$array_where['user_email'] = $email;
$sql_fetch = "SELECT * FROM $table WHERE `user_email`='$email'";
$row = $wpdb->get_results( $sql_fetch, ARRAY_A );
if ( sizeof( $row ) != 0 ) {
echo '0';
} else {
echo '1';
}
die();
}
How to make the code work in online server ?
What you’re experiencing (AJAX works locally, but not on the server) there is a delay problem. Locally everything works that fast, that you can’t see your problem. In short, this is your problem:
So you need to find a way how to tell your Callback (B) that it has to wait for (A). Here’s how:
Register Scripts and move data from PHP to JS
Register and enqueue and localize your data the right way: Wrap it in a function or method and hook it to
wp_enqueue_scripts
(public/themes),login_enqueue_scripts
(password/login/register) oradmin_enqueue_scripts
. The usewp_localize_script()
to move data from PHP to JS and make it accessible there.How to use jQuery AJAX the proper way.
There’re several functions you can use: The default
$.ajax({});
function or their shortcuts$.post();
,$.getJSON();
, etc.So you can simply use something like the following – using the
success/fail
object methods.If you want to go more in depth and do things really the right way, you’ll have to use method chaining. (There’s still room for improvement left).
Note: For better examples of the wrapper around the callback, take a look at commonjs or AMD and their difference.
Waiting for other AJAX responses
The interesting – and most powerful part of the whole jQuery (and other libararies) AJAX handling – question is how to wait until A is finished to then start B and its processing. The answer is “deferred” loading and “promises”.
I’ll add a quick example. You should maybe think about building and object and separating stuff by appending it via
this.
to the object, but for an example the following should be enough:Example (A) This basically like I do it. You’ll have to fill the bits your own.
Example (B) I never tried it like this, but it should work as well. Easier to read, but I like
$.when()
resolved promises more.If you want to get even more in depth, read the Docs about
deferred
andthen
.async/ await
EDIT As this answer receives quite some attention over the years and already is 8 years old, please take a look at the following snippet.
Keep in mind, that
async/await
still deals with Promises. It’s just synthetic sugar. Also keep in mind whatthis
refers to, when using short hand functions like seen inhandlerB()
.That will even work with jQuery.
For executing one AJAX request after finishing another AJAX request, you have to add the below one line,
i.e, you need to add
async: false
in the ajax method as below,