How to redirect the user to the home page with jQuery after successful login
<p>I'm new to jQuery and I'm using the post method in my <strong>login.js</strong> file to send a request to my <strong>login.php</strong> script and retrieve an error message depending on the nature of the form data that has been inputted.</p>

<p>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.</p>

<p>Any ideas where I'm going wrong?</p>

<p><strong>login.js</strong>:</p>

<pre><code>$(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';
            }
        });
    });
});
</code></pre>

<p><strong>login.php</strong>:</p>

<pre><code>&lt;?php 

include "core/init.php";

$username = $password = "";

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

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

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

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

?&gt; 
</code></pre>

<p>My <strong>log_in</strong> function:</p>

<pre><code>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;
        }
    }
}
</code></pre>