0

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 [email protected] 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?

1 Answer 1

0

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.

3
  • Ah, makes sense. Would it not work to simply reference where the function is? Or not good practise?
    – zilj
    Oct 17, 2012 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.
    – fuxia
    Oct 17, 2012 at 15:41
  • 1
    For anyone else interested, I ended up following w4dev.com/wp/wp_ajax to get it all working. Thanks toscho for the solution.
    – zilj
    Oct 17, 2012 at 16:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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