I'm trying to implement email validation using the Bassistance jQuery plugin, with a check if the email adress exists.
The remote script returns correcctly true
or false
, however - no error message is shown. Other validation (required, email) works fine.
Any idea's what I'm missing here?
The jQuery code:
jQuery().ready(function() {
jQuery("#post").validate({
rules: {
lid_email: {
required: true,
email: true,
remote: {
type: 'POST',
url:"email-check.php"
}
}
},
messages: {
lid_email: {
required: 'Gelieve een geldig e-mailadres in te vullen.<br>',
email: 'Gelieve een geldig e-mailadres in te vullen.<br>',
remote: 'Dit adres bestaat reeds. Gelieve een ander adres te kiezen.'
}
}
})
jQuery('#lid_email').blur(function() {
jQuery("#post").validate().element( "#lid_email" );
});
});
And the remote script:
<?php
header('Content-type: application/json');
require('../../../wp-blog-header.php');
$request = trim(strtolower($_POST['lid_email']));
if ( email_exists($request) == TRUE ) {
echo json_encode(FALSE);
} else {
echo json_encode(TRUE);
}
?>
I found the solution:
I'm using the script in a Wordpress site, and included wp-blog-header.php in order to have access to the Wordpress functions.
Apparently the line require('../../../wp-blog-header.php
breaks the json functionality
If I remove this line, the booleans are sent correctly, and the error message shows up.
All I have to do, is write my own user_exists-function, and then the problem should be solved.
Have you tried with:
echo (email_exists($request) === true )?'true':'false'
instead of:
if ( email_exists($request) == TRUE ) {
echo json_encode(FALSE);
} else {
echo json_encode(TRUE);
}
I have a similar problem and I resolved in this way. Additionally, I commented to you that in this way I didn't need put a json header.