It is irritating me why AJAX response is 0 !. I want to check whether the email address is valid or invalid on blur.Please suggest me here is my code This is my php code

    <input type="text" name="email_customer"  id="email_customer"/>
<div id="email_info"></div>
function add_send_msg(){
    $email = isset($_POST['email_customer']) ? $_POST['email_customer'] : null; 
    if( $email ){
        $msg = 'valid';
    echo $msg;

Adding js and localize script

    add_action( 'init', 'my_script_enqueuer' );

function my_script_enqueuer() {
   wp_enqueue_script( "send_email", path_join(WP_PLUGIN_URL,basename(dirname(__FILE__)).'/send_email.js', array('jquery')));
   wp_localize_script( 'send_email', 'myAjax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' )));        

and js file send_email.js

jQuery(document).ready(function () {
    jQuery("input[name='email_customer']").blur(function (){
        dataString = "email_customer="+jQuery(this).attr("value")+"&action=send_message_to_customer";

            type: "POST",
            data: "email_customer="+jQuery(this).attr("value")+"&action=send_message_to_customer",
            url: myAjax.ajaxurl,
            beforeSend: function(){
                jQuery("#email_info").html("Checking Email......");
            success: function(data) {
                if( data == 'valid'){
                jQuery("#email_info").html("Email Ok");

                jQuery("#email_info").html("You have entered invalid email");



  • Where is myajax_script registered? Also, if you are logged in, you have to add the line add_action('wp_ajax_nopriv_send_message_to_customer','add_send_msg');
    – Tomás Cot
    Sep 6 '14 at 19:51

First, don't enqueue on the init action. Enqueue on the wp_enqueue_scripts action for the front end, or the admin_enqueue_scripts action for the back end.

Second, the wp_ajax_* action will only work for logged in users. For non-logged in users, you must use the wp_ajax_nopriv_* action instead. If it needs to work for both, then you must hook both.

Finally, your jQuery.ajax call is missing the "action" of "send_message_to_customer" and the data is in the wrong format. Should look like this:

data: ['email_customer' : jQuery(this).attr("value") ], action: 'send_message_to_customer',

Or possibly just jQuery(this).val() instead of the attr() call there. Dunno, didn't try the code myself to test that.

  • Still Not Working .. Sep 7 '14 at 3:13
  • So, post the new code that doesn't work in your question. The answer is correct, but I can't know what you changed unless you tell me.
    – Otto
    Sep 7 '14 at 3:19
  • Yes I think Code is correct but I am unable to know why AJAX response 0 Sep 7 '14 at 3:44

Try to write the 'data' and 'action' like this -

action: 'send_message_to_customer'

data:{'email_customer' : jQuery(this).val()}

Add one more hook -

add_action( 'wp_ajax_nopriv_send_message_to_customer', 'add_send_msg' );

  • Still Not Working , Can You please try yourself , if you don't mind... Sep 7 '14 at 3:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.