jquery form plugin redirect instead of printing response in html on successful php mysql script

93 Views Asked by At

Want to redirect to some page on successful login I am using jQuery form plugin to submit my form. Everything works but response gets printed instead of redirect.

Login Form Submit using jquery form plugin

<script>
    $(document).ready(function(){
        // Form submit via jQuery form plugin
        $('#formUserSignIn').ajaxForm(function(response){
            if(parseInt(response) !== 1){
            $('#flashMessage').html(response);              
            }
            else{
                window.location.href='/apps/home/dashboard';
            }
        });
    });
</script>

process.php

    if(isset($_POST['btnUserSignIn'])){
    $userEmail  = mysqli_real_escape_string($conn, $_POST['txtUserEmail']);
    $userPass   = mysqli_real_escape_string($conn, $_POST['txtUserPassword']);
    $userPass   = $encrypt->hash_pass($userPass);

    $sql_check_user = $conn->query("SELECT * FROM workflow_users 
        WHERE user_email = '$userEmail' AND user_pass = '$userPass'") 
    or die(query_error('queryCheckUser', $conn->error));

    if($sql_check_user->num_rows > 0){
        $row_check_user = $sql_check_user->fetch_array();
        $userName = $row_check_user['user_name'];

        /*
        |-----------------------------------------------------
        | SET PHP SESSION FOR CURRENT USER
        |-----------------------------------------------------
        */
        // Set session name
        session_name('currentuser');
        // Set session expire time
        ini_set('session.gc_maxlifetime', 7200);
        // Start php session
        session_start();
        // Store query result to session array
        $_SESSION['user_id']        = $row_check_user['user_id'];
        $_SESSION['user_name']      = $row_check_user['user_name'];
        $_SESSION['user_email']     = $row_check_user['user_email'];
        $_SESSION['user_pass']      = $row_check_user['user_pass'];
        $_SESSION['user_type']      = $row_check_user['user_type'];
        $_SESSION['user_status']    = $row_check_user['user_status'];
        $_SESSION['user_otp']       = $row_check_user['user_otp'];

        return 1;
    }
    else{
        $msg->set_flash_msg("Error", "Incorrect email or password.");
    }


}
1

There are 1 best solutions below

4
On

You need to do echo when you call ajax instead of return

if(isset($_POST['btnUserSignIn'])){
    $userEmail  = mysqli_real_escape_string($conn, $_POST['txtUserEmail']);
    $userPass   = mysqli_real_escape_string($conn, $_POST['txtUserPassword']);
    $userPass   = $encrypt->hash_pass($userPass);

    $sql_check_user = $conn->query("SELECT * FROM workflow_users 
        WHERE user_email = '$userEmail' AND user_pass = '$userPass'") 
    or die(query_error('queryCheckUser', $conn->error));

    if($sql_check_user->num_rows > 0){
        $row_check_user = $sql_check_user->fetch_array();
        $userName = $row_check_user['user_name'];

        /*
        |-----------------------------------------------------
        | SET PHP SESSION FOR CURRENT USER
        |-----------------------------------------------------
        */
        // Set session name
        session_name('currentuser');
        // Set session expire time
        ini_set('session.gc_maxlifetime', 7200);
        // Start php session
        session_start();
        // Store query result to session array
        $_SESSION['user_id']        = $row_check_user['user_id'];
        $_SESSION['user_name']      = $row_check_user['user_name'];
        $_SESSION['user_email']     = $row_check_user['user_email'];
        $_SESSION['user_pass']      = $row_check_user['user_pass'];
        $_SESSION['user_type']      = $row_check_user['user_type'];
        $_SESSION['user_status']    = $row_check_user['user_status'];
        $_SESSION['user_otp']       = $row_check_user['user_otp'];

        echo 1; // echo 1 if success
    }
    else{
        echo "error"; // echo error or any thing else if there is an error
        $msg->set_flash_msg("Error", "Incorrect email or password.");
    }


}