In my WordPress v5.8.2, I have localized the ajax_url
in the functions.php:
wp_localize_script('site_admin_ajax', 'site_ajax', array('ajax_url' => admin_url('admin-ajax.php'), 'check_nonce' => wp_create_nonce('site_ajax_nonce')));
With the below jQuery script I am processing the form to check if the email ID from the HTML form is already existed in the WordPress:
$(document).on("submit", "#form", function(e) {
e.preventDefault();
$email = $(this).find('input[name=email]').val(); // email
//ajax request, check if user exists
$.ajax({
type: "GET",
dataType: 'json',
url: site_ajax.ajax_url,
data: {
action: 'email_exists_check',
security: site_ajax.site_ajax_nonce
},
success: function(data) {
if (data.result) {
alert('Email exists!');
} else {
alert('Email doesn't exsists!');
}
}
});
});
Below the PHP code in separate file to check email:
add_action('wp_ajax_email_exists_check', 'email_exists_check');
add_action('wp_ajax_nopriv_email_exists_check', 'email_exists_check');
function email_exists_check() {
$email = $_GET['email'];
// do check
if (email_exists($email)) {
$response - > result = true;
} else {
$response - > result = false;
}
// echo json
echo json_encode($response);
}
The above entire code is not able to alert if email exists.
I have tested the code form_email_exists_check()
without ajax and is working fine. How can I make this code work?
Update #1
This is how the js file is enqueued, which contains the above jQuery script along with other scripts:
wp_enqueue_script('site-scripts', get_stylesheet_directory_uri() . '/assets/js/site-scripts.js', array('jquery'), null, true);
Tried enqueuing as below inline with wp_localize_script()
document:
wp_enqueue_script('site_admin_ajax', get_stylesheet_directory_uri() . '/assets/js/site-scripts.js', array('jquery'), null, true);
Still this did not worked.
I am getting this error in the console Uncaught ReferenceError: site_ajax is not defined
, means the admin-ajax.php
is not loading and result is 0.
CodePudding user response:
Try parsing JSON response like this :
$(document).on("submit", "#form", function(e) {
e.preventDefault();
$email = $(this).find('input[name=email]').val(); // email
//ajax request, check if user exists
$.ajax({
type: "GET",
dataType: 'json',
url: site_ajax.ajax_url,
data: {
action: 'email_exists_check',
security: site_ajax.site_ajax_nonce
},
success: function(data) {
var status = JSON.parse(data);
if (status.result === true) {
alert('Email exists!');
} else {
alert('Email doesn\t exsists!');
}
}
});
});
Also , do not forget die()
after AJAX call in WordPress
<?php
add_action('wp_ajax_email_exists_check', 'email_exists_check');
add_action('wp_ajax_nopriv_email_exists_check', 'email_exists_check');
function email_exists_check() {
$response = array();
$email = $_GET['email'];
// do check
if ( email_exists( $email ) ) {
$response['result'] = true;
} else {
$response['result'] = false;
}
// echo json
echo json_encode( $response );
die();
}
CodePudding user response:
Your Enqueue Script is ok, but you have some issues in the Ajax and PHP. Without having your complete implementation, I can't test this, but it should work.
PHP:
add_action('wp_ajax_email_exists_check', 'email_exists_check');
add_action('wp_ajax_nopriv_email_exists_check', 'email_exists_check');
function email_exists_check() {
// Check nonce and email is set by ajax post.
if ( isset( $_POST['email'] ) && wp_verify_nonce( 'check_nonce', 'security' ) ) {
// Sanitize your input.
$email = sanitize_text_field( wp_unslash( $_POST['email'] ) );
// do check.
if ( email_exists( $email ) ) {
$response = true;
} else {
$response = false;
}
// send json and exit.
wp_send_json( $response );
}
}
And your jQuery:
$( document ).on( "submit", "#form", function(e) {
e.preventDefault();
$email = $( this ).find( 'input[name=email]' ).val(); // email
// ajax request, check if user exists.
$.ajax({
type: "POST",
dataType: 'json',
url: site_ajax.ajax_url,
data: {
email: $email,
action: 'email_exists_check',
security: site_ajax.check_nonce
}
})
.done(function(data) {
if (data.result) {
alert( 'Email exists!' );
} else {
alert( 'Email doesn\'t exsist!' );
}
});
});