如何在使用 PreventDefault() 的情况下进行重定向页面?

问题描述 投票:0回答:1

第一个问题是,如果我放置 e.preventDefault,它在成功登录时不会重定向到 homeadmin.php,因为它成功调用 ajax 并且我的验证没有错误,它可以验证,

第二个问题是我需要使用 e.preventDefault() 来显示客户端验证的错误消息,这与第一个问题相矛盾

这是我只关心使用 e.preventDefault() 并且验证没有问题,我只想在成功登录后重定向到其他页面并在验证表单时显示验证错误

我想要的结果是,当用户成功登录时,它会重定向到其他页面,当用户登录时出错时,它不会重定向到其他页面,而是显示错误消息验证

$(document).ready(function(){
    $('#loginformid').on('submit',function(e){
        e.preventDefault();
    
        var formdata = new FormData(this);
        
        $.ajax({
            type: 'POST',
            url: '../controller/loginquery.php',
            data: formdata,
            dataType: 'json',
            processData: false,
            contentType: false,
            success: function(respoadd) {
                if (respoadd.success === 'login') {
                    window.location.replace('../admin/homeadmin.php');
                } else {
                    $('#username-error').html(respoadd.username_error);
                    $('#password-error').html(respoadd.password_error);
                }
            },
            error: function(err) {
                console.log(err);
            }
        });
    })
})
$username = mysqli_real_escape_string($connection, $_POST['username']);
$password = mysqli_real_escape_string($connection, $_POST['password']);

$username_error = '';
$password_error ='';
$success = '';


try{
    if(strlen($username) < 5 || strlen($username) > 255){
        $username_error = "This field require with 5 minimum character not greater than 255";
    }
    if(strlen($password) < 5 || strlen($password) > 255){
        $password_error = "This field require with 5 minimum character not greater than 255";
    }
    if($username_error === '' && $password_error === ''){
        $queryvalidate = "SELECT * FROM tbl_userinfo WHERE username = ?";
        $stmt = mysqli_prepare($connection, $queryvalidate);
        mysqli_stmt_bind_param($stmt, "s", $username);
        mysqli_stmt_execute($stmt);
        $result = mysqli_stmt_get_result($stmt);
        if($result && mysqli_num_rows($result) === 1){
            $user_data = mysqli_fetch_assoc($result);
            $stored_password_hash = $user_data['password'];
            if(password_verify($password, $stored_password_hash)){
                $success = "login";
                $_SESSION['status'] = 'valid';
                echo "<script>window.location.href ='../admin/homeadmin.php'</script>";
            }
            else{
                $password_error = "The password is not match with useranme";
                $_SESSION['status'] = 'invalid';
            }
        }
        else{
            $username_error = "Username does not exist";
            $_SESSION['status'] = 'invalid';
        }
        mysqli_stmt_close($stmt);
        mysqli_close($connection);
    }
    $output = array(
        'username_error' => $username_error,
        'password_error' => $password_error,
        'success' => $success
    );
    echo json_encode($output);   

}catch(Exception $e){
    echo 'error page';
}
javascript php jquery ajax
1个回答
0
投票

示例=

document.querySelector("#test").addEventListener("click",e=>{
    e.preventDefault()
    location.href = "/test"
    
})
© www.soinside.com 2019 - 2024. All rights reserved.