简体   繁体   中英

jquery validate - remote always returns true

I have a form and I am using jQuery validate plugin.

I am also using remote to check if the username and email are available or not but I always get false back from the script.

here is the jquery validation part:

$(document).ready(function () {
    $('#registration').validate({
        rules: {
            email: {
                email: true,
                remote:{
                    url: "./php/checkemail.php",
                    type: "POST",
                }
            },
            username: {
                minlength: 4,
                maxlength: 16,
                remote: {
                    url: "./php/checkusername.php",
                    type: "POST",
                }
            },
            messages: {
                email: {
                    email: "Please enter a valid e-mail address.",
                    remote: "This email is already registered.",
                },
                username: {
                    minlength: "Your username must be at least 4 characters long but less than 16",
                    maxlength: "Your username must be at least 4 characters long but less than 16",
                    remote: "This username is already registered.",
                },
                submitHandler: function(form) { 
                    $('#registration').ajaxSubmit();
                    return false;
                },
            });
        });

and here is the checkemail.php file:

$mysqli = mysqli_init();
if(isset($_POST['email'])) {
    $email = mysqli_real_escape_string(strtolower($_POST['email']));
    $check= mysqli_query("SELECT * FROM users WHERE LOWER email = '$email'");
    if(mysqli_stmt_num_rows($check)!=0) {
        echo json_encode(false);
    } else {
        echo json_encode(true);
    }
}

Can anyone tell me please what I did wrong and how to fix it?

Also, even if I change the script to send me true, the remote message from the validation script doesn't appear on my form.

I think the remote function is checking for a string.

So try this:

if(mysqli_stmt_num_rows($check)!=0) {
    echo 'false';
} else {
    echo 'true';
}

So, the problem was that the php i used was deprecated and when i changed it to the new version i did it wrong... so the correct way of doing it is this:

if(isset($_POST['email']))

{

$email = mysqli_real_escape_string($link, $_POST['email']);

$check= mysqli_query($link, "SELECT * FROM users WHERE email = '".$email."'") or die(mysqli_error($link));

if(mysqli_num_rows($check)!=0)

{

 echo 'false';

}else{

 echo 'true';

}

}

mysqli_close($link);

?>

where $link is the connection to the mysql database :

$link = mysqli_connect("$db_host", "$db_username", "$db_password", "$db_database") or die ("Error " . mysqli_error($link));

and now it works just fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM