简体   繁体   中英

How to redirect the user to the home page with jQuery after successful login

I'm new to jQuery and I'm using the post method in my login.js file to send a request to my login.php script and retrieve an error message depending on the nature of the form data that has been inputted.

All my error messages are working and are correctly displayed in the browser. My problem is that when the user enters valid login credentials, I want to redirect them to a home page. Instead, jQuery is taking my result for a successful login and inserting it into login_error div.

Any ideas where I'm going wrong?

login.js :

$(document).ready(
function login_error()
{
    $("#login").click(function(event)
    {
        event.preventDefault();
        var postData = $('#login_form').serialize();

        $.post('login.php', postData, function(result)
        {   
            if(result != "match"){
                $('#login_error').html(result);
            }else{
                window.location = 'home.php';
            }
        });
    });
});

login.php :

<?php 

include "core/init.php";

$username = $password = "";

if(isset($_POST['username']) && isset($_POST['password'])){

    $username = $_POST['username'];
    $password = $_POST['password'];

    if(empty($username) || empty($password)){
        echo '<p class="alert-danger">You didn\'t fill in all the fields.</p>';
    }else if(user_exists($username) === false){ 
        echo '<p class="alert-danger">The username you entered doesn\'t match any account in our records. Sign up for an account.</p>';
    }else if(user_verified($username) === false){
        echo "<p class='alert-danger'><strong>".$username."</strong>&nbspis not a verified username. Check your inbox for a verification email.</p>";
    }else if(check_details($username, $password) === false){
        echo '<p class="alert-danger">The login details you entered don\'t match our records.</p>';
    }else{
        log_in($username, $password);
    }

}else{
    echo '<p class="alert-danger">Oops - something broke! We\'re working to fix this, check back soon.</p>';
}

?> 

My log_in function:

function log_in($username, $password){

    $db   = mysqli_connect('localhost', 'root', '', 'forum');
    $sql  = "SELECT user_id FROM users WHERE username = ?";
    $stmt = mysqli_prepare($db, $sql);

    if($stmt){
        mysqli_stmt_bind_param($stmt, 's', $username);
        mysqli_stmt_bind_result($stmt, $user_id);
        mysqli_stmt_execute($stmt);
        mysqli_stmt_fetch($stmt);

        if(!empty($user_id)){
            $_SESSION['id']   = $user_id;
            $_SESSION['user'] = $username;
            echo 'match';
        }else{
            return false;
        }
    }
}

If your remaining code working fine then Just change window.location code with window.location.href = "home.php"; Here href used for location redirection

You could just use header("Location: home.php"); in the php login function on success. Like:

    if(!empty($user_id)){
        $_SESSION['id']   = $user_id;
        $_SESSION['user'] = $username;
        header("Location: home.php");
    }else{
        return false;
    }

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