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 have setup a jQuery keyup delay function to check the email typed into an input field.

It works fine after testing with an alert('Key pressed!');

But I want it to say, for example, 'Yes this email is associated with a user' OR 'Sorry, this email is not in our database' without submitting any page requests.

    var delay = (function(){
        var timer = 0;
        return function(callback, ms){
            clearTimeout (timer);
            timer = setTimeout(callback, ms);
        };
    })();
    $('input').keyup(function() {
        delay(function(){
            // check email exists and output result
            // Can I use PHP here? 
            // Can ajax/json help?
        }, 500 );
    });

I tried to check with php but this is rendered before the jQuery of course which is where I'm storing my email var.

After a few google searches it sounds like this may not be possible. Can anyone confirm or offer a solution?

Thanks

share|improve this question
    
Since the keyup event is fired every time a keyboard key is released, I would recommend using the blur event which fires only when the user moves to the next field. You should be able to attache the event to an ajax call that will check the existence of the email address. –  nvwd Oct 17 '12 at 11:36
1  
This is not WordPress specific. –  Joseph Oct 17 '12 at 12:13
    
Perhaps not strictly, but I had the original intention of using <?php if ( email_exists($email) ) { . . . } ?> which is wordpress specific. So I suppose you could generalise my question as, can I use jquery/json/ajax with if ( email_exists($email). –  Graeme Oct 17 '12 at 12:52

1 Answer 1

up vote 2 down vote accepted

I would use an AJAX request to a PHP script that does the lookup, which might look something like this on the jQuery side after document ready:

// jquery
$('#email-input').live('change', function() {
    //ajax request
    $.ajax({
        url: "email_check.php",
        data: {
            'email' : $('#email-input').val()
        },
        dataType: 'json',
        success: function(data) {
            if(data.result) {
                alert('Email exists!');
            }
            else {
                alert('Email doesnt!');
            }
        },
        error: function(data){
            //error
        }
    });
});

With the example above your PHP would have to return json data once its done the email check based on the address, for example your email_check.php would contain:

// get email passed via AJAX
$email = $_GET['email'];

// do check
if ( email_exists($email) ) {
    $response->result = true;
}
else {
    $response->result = false;
}

// echo json
echo json_encode($response);

Hope that helps!

share|improve this answer
    
I was coming to the conclusion I need to use ajax, this looks like a good solution. I'm going to try this out now. –  Graeme Oct 17 '12 at 12:55
    
Sorry, I'm a bit of a noob with ajax. Should the jquery be inside my keyup function? I added an if the email exists as a simple test, to my email_check.php outputting either true or false. This isn't right is it? if ($email) {$response->result = true;} else {$response->result = false;} –  Graeme Oct 17 '12 at 13:14
    
Sorry, I have now solved this thanks to your code. I forgot the url: needed to account for the theme url. –  Graeme Oct 17 '12 at 13:23
    
Damn. Unfortunately, within my email_check.php I wanted to run if ( email_exists($email) ) and then return the true or false value. It's causing a server error and I can't seem to get it to work. I tested it within the origin page and it is working. Why not within the php target page? –  Graeme Oct 17 '12 at 14:09
    
Re: your questions about where to use it, just use it within document ready, no need to put it in your keyup function, this function I've written does just that (on change basically). As for the server error your getting on the PHP page can you provide a link? Can you show the code within the email_exists function? –  Mateusz Michalik Oct 18 '12 at 1:48

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.