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'm requesting the wordpress function if (email_exists($email)) { ... } from a file (email_check.php) via ajax. Using this function is causing a server error though.

The origin (from an input text field):

var check = $('#email-input');
var email = check.val();
$.ajax({
    url: "email_check.php",
    data: {'email' : email},
    type: "GET",
    dataType: 'json',
    success: function(data) {
        if(data.result) {
            alert('Email exists!');
        }
        else {
            alert('Email doesnt!');
        }
    },
    error: function(data){
        //error
    }
});

check_email.php

$email = $_GET['email'];
if ( email_exists($email) ) {$response->result = true;} else {$response->result = false;}
echo json_encode($response);    

This results in no alert taking place. By going directly to check_email.php?email=email@address.com it give me a server error page. I also replaced email_exists($email) with the same direct email entered as the url param and it does work.

Does anyone know why this is?

share|improve this question

1 Answer 1

up vote 0 down vote accepted

This happens because the PHP file you are using has no connection to WordPress, the function is never loaded. Use the built-in AJAX-API instead, and it will work fine.

share|improve this answer
    
Ah, makes sense. Would it not work to simply reference where the function is? Or not good practise? –  Graeme Oct 17 '12 at 15:38
    
Most WordPress files require other WordPress files to be included without an error. In the end you would include almost as many files as the AJAX file already does. –  toscho Oct 17 '12 at 15:41
    
Ok thanks a lot. –  Graeme Oct 17 '12 at 15:42
1  
For anyone else interested, I ended up following w4dev.com/wp/wp_ajax to get it all working. Thanks toscho for the solution. –  Graeme Oct 17 '12 at 16:59

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.