1

I'm trying to check on email field blur, if the email already exists in db. My code is now:

<script type="text/javascript">
    $(document).ready(function () {
        // Validation
        $("#soutez").validate({
            rules: {
                   email: {
                    required: true,
                    email: true,
                    remote: "check-email.php",
                },

            },
            messages:{
                email:'Email address exists.'
            },
            onkeyup: false,
            onblur: true,
      });

     });
</script>

And the php code is

$email= $_GET['email'];
echo $email;
$path = $_SERVER['DOCUMENT_ROOT'];

include_once $path . '/wp-config.php';
include_once $path . '/wp-load.php';
include_once $path . '/wp-includes/wp-db.php';
include_once $path . '/wp-includes/pluggable.php';
global $wpdb;

$email_exists = $wpdb->get_row('SELECT COUNT(*) as count from reg_form_new WHERE email = "'.$email.'"');

if ( $email_exists->count == 0 ) { echo 'true'; } else { echo 'false'; }

exit; }

The php code returns true/false correctly, but for some reason it doesn't work with the jQuery script. Anyone can tell me what I'm doing wrong? Thanks

5
  • is not that easy because e.g [email protected], [email protected], [email protected], [email protected], etc... are all the same email Commented Jun 18, 2013 at 22:25
  • what does the echo $email do in the line 2 of PHP code ? Commented Jun 18, 2013 at 22:27
  • just tested if I get the email... Commented Jun 18, 2013 at 22:30
  • Wouldn't the echo $email line make the return "<email address>true" instead of just "true" Commented Jun 18, 2013 at 22:46
  • I tried return true, which doesn't work neither.. Commented Jun 19, 2013 at 6:32

2 Answers 2

4

You have to echo exactly the string 'true' and nothing else if you want it to be allowed through. Anything else ('false',null,undefined, or any string) will be interpreted as invalid and if there is a string, that string will be displayed.

So check carefully that your php script is printing out only 'true' or 'false' for now. The script as you've shown it prints the email followed by 'true' or 'false'. This will always be interpreted as false.

Read the documentation of the remote method:

The response is evaluated as JSON and must be true for valid elements, and can be any false, undefined or null for invalid elements, using the default message; or a string, eg. "That name is already taken, try peter123 instead" to display as the error message.

Use Firebug or Chrome Inspector to verify that your response is correct.

Sign up to request clarification or add additional context in comments.

Comments

0

I'm not an expert in Jquery, you can to adapt the working function to your need:

$.ajax({
            url: "check-email.php",
            type:'GET',
            /*you my specify dataType: 'json', for example ...*/
            data: $("#soutez").serialize(), //email input id 
            success: function(res) { 
                switch(res) {
                    case ('true'):
                        alert("true");  
                        break;

                    case ('false'):
                        alert('false');
                        break;
                                    // other cases...

                }
            } 

      });

3 Comments

I'm trying to use the jQuery validation plugin
What if the user disable the Javascript ?the best approach is to add a PHP validation, something like : if(!filter_var($email, FILTER_VALIDATE_EMAIL)) echo "Email Not Valid ! ";
I of course do have PHP validation after the form submission. What I want is to inform the user that the email exists when filling the form.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.