Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am pulling my hair out trying to figure this out. I have followed the wordpress codex here for AJAX: http://codex.wordpress.org/AJAX_in_Plugins

All I keep returning is 0, I never get a 1. Any idea why? My code is as follows.

PHP:

add_action('wp_enqueue_scripts', 'live_validation' );
add_action('wp_ajax_validate_email', 'validate_email_input');
add_action('wp_ajax_nopriv_validate_email', 'validate_email_input');

function live_validation() {
    wp_enqueue_script( "validate_email", STRIPE_BASE_URL . 'inc/js/check-email.js', array( 'jquery' ) );
    wp_localize_script( "validate_email", "validateEmail", array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
}

function validate_email_input() {
    $email = $_POST['email'];
    if ( email_exists($email) ) {
        echo 1; 
    } else {
        echo 0;
    }
    exit;
}

JS:

jQuery(document).ready(function($) {
    $('input[name=email]').keyup(function() {

        var input_value = $(this).val();

        $.post( validateEmail.ajaxurl, { action:'validate_email', email:input_value }, function(data) {
            $('.message').html(data);
        });
    });
});

HTML:

<div class="form-row">
    <label for="email"><?php _e('Email', 'oe'); ?></label>
    <input type="text" size="20" id="email" name="email" />
    <span class="message"></span>
</div>
share|improve this question

2 Answers 2

I'll guess that you're testing this by entering your admin email address. Have a look at the Codex page for email_exists():

If the E-mail exists, function returns the ID of the user to whom the E-mail is registered.

If you're entering the email address of the first user created for a site, the user ID will be 0, so if ( email_exists($email) ) for that user will evaluate false. The example given on that page does not account for this situation.

share|improve this answer
    
I wish that was the case :/ I am entering a "test" user's email I created. –  ftntravis Jul 4 '13 at 1:56
    
well your code otherwise works for me. –  Milo Jul 4 '13 at 1:56
    
Weird. Any idea what else could be causing an issue? –  ftntravis Jul 4 '13 at 1:57
    
not sure. I would start by just echoing back the email address to make sure the function is getting called, the 0 could be an error before the email_exists check happens, or it could be the 0 you're echoing yourself. –  Milo Jul 4 '13 at 2:01
    
I don't believe the function is getting called I tried just echoing the address with no luck. Trying to figure out how to debug this. –  ftntravis Jul 4 '13 at 2:03
up vote 0 down vote accepted

Found the problem. It always helps to read the codex thoroughly!

"ATTENTION: When you are writing a plugin (in plugin wp_ajax usage) place lines below in your main plugin file, e.g. myplugin.php"

http://codex.wordpress.org/Plugin_API/Action_Reference/wp_ajax_%28action%29

Just make sure all of your AJAX requests go in your main plugin file.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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