jQuery Validate Remote redirect page on false

为君一笑 提交于 2019-12-12 03:54:41

问题


I am using jQuery Validate remote to check a php file and see if an email address is already in use. It works fine, however, I need it to redirect to a new page IF the email typed in already exists or "false". Below is the working remote validate script and simple php file. Again this works fine, just not sure how to redirect to a new page if the php script returns false.

     rules: {
            fullname: "required",
            email: {
                required: true,
                email: true,
                remote: {
                    url: "validate-email.php",
                    type: "post",
                },
            },

    messages: {
     email: {
        required: 'Please enter a valid email address',
        remote: 'Sorry, this email address is already in use',
        }
    }

validate-email.php

$email = filter_input(INPUT_POST, 'email', FILTER_SANITIZE_EMAIL);
$prep_stmt = "SELECT id FROM members WHERE email = ? LIMIT 1";
$stmt = $mysqli->prepare($prep_stmt);

if ($stmt) {
    $stmt->bind_param('s', $email);
    $stmt->execute();
    $stmt->store_result();

    if ($stmt->num_rows == 1) {
        // A user with this email address already exists
        echo 'false';
    }else{
        echo 'true';
    }
}

Just to mention it one last time, the above php and jquery script work fine. Just need to redirect to a new page if the php result is echo 'false';


回答1:


I was able to redirect by simply adding the information from TheMintyMate

 rules: {
        fullname: "required",
        email: {
            required: true,
            email: true,
            remote: {
                url: "validate-email.php",
                type: "post",
                success: function(e) { if(e == false){window.location.replace("http://website");}}
            },
        },

messages: {
 email: {
    required: 'Please enter a valid email address',
    remote: 'Sorry, this email address is already in use',
    }
}


来源:https://stackoverflow.com/questions/35372364/jquery-validate-remote-redirect-page-on-false

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