I\'ve successfully created a form that submits and adds users to a mysql database, and form validation with \'jQuery Validator\' plugin works great for everything except che
For WordPress & PHP
Most important thing is instead or return true or false, need to echo 'true' or 'false'
jQuery or JS
email: {
required: true,
minlength: 6,
email: true,
remote:{
url : 'your ajax url',
data: {
'action': 'is_user_exist',
},
},
},
WordPress or PHP Backend Code
In backend you will automatically get the value of field via GET method.
/*
*@ Check user exists or not
*/
if( !function_exists('is_user_exists_ajax_function') ):
function is_user_exists_ajax_function() {
$email = $_GET['email'];
if( empty($email) && is_email($email) ):
wp_send_json_error( new \WP_Error( 'Bad Request' ) );
endif;
$is_email = email_exists( $email );
if($is_email):
echo 'false';
else:
echo 'true';
endif;
wp_die();
}
add_action( 'wp_ajax_is_user_exist', 'is_user_exists_ajax_function' );
add_action( 'wp_ajax_nopriv_is_user_exist', 'is_user_exists_ajax_function' );
endif;