jquery validate - remote always returns true

99封情书 提交于 2019-12-24 17:17:30

问题


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.


回答1:


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';
}



回答2:


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.



来源:https://stackoverflow.com/questions/21681507/jquery-validate-remote-always-returns-true

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!