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 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?

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.

  • Ah, makes sense. Would it not work to simply reference where the function is? Or not good practise? – zilj 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. – fuxia Oct 17 '12 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 '12 at 16:59

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.