Page redirection with inconsistent JS and PHP

Asked

Viewed 54 times

1

I am trying to redirect to a page using window.location.replace(). However, sometimes the page does not redirect. At first I thought the error was loading a google source, but even removing, the problem persisted.

JS/jQuery code

$('#button_login').on('click', function() {
    var login = $('#inputLogin').val();
    var pass = $('#inputPassword').val();
    var remember = $('#remember').is(':checked') ? $('#remember').val() : 'off';


    if (login != '' && pass != '') {
        $.ajax({
            url: 'functions/signin.php',
            method: 'POST',
            cache: false,
            data: {
                login: login,
                pass: pass,
                remember: remember
            },
            success: function(data, status, request) {
                if (data == "OK") {
                    window.location.replace("index.php");

                } else {

                }
            },
            error: function(request, status, error) {

            }
        });
    } else {
        $('#inputLogin')[0].oninvalid = function(event) {
            event.preventDefault();
        }
        $('#inputPassword')[0].oninvalid = function(event) {
            event.preventDefault();
        }
        $('#alert-login').html('Preencha os campos');
        $('#alert-login').fadeTo(2000, 500).slideUp(1000, 
            function() {
                $('#alert-login').slideUp(500);
            });
    }
});

function getCookie(cname) {
    var name = cname + "=";
    var decodedCookie = decodeURIComponent(document.cookie);
    var ca = decodedCookie.split(';');
    for(var i = 0; i <ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') {
            c = c.substring(1);
        }
        if (c.indexOf(name) == 0) {
            return c.substring(name.length, c.length);
        }
    }
    return "";
}

$(document).ready(function() {
    var login = getCookie('login');
    var pass = getCookie('pass');
    var remember = getCookie('remember');
    if (remember != '') {
        $('#inputLogin').val(login);
        $('#inputPassword').val(pass);
        $('#remember').click();
    }
});

PHP code

<?php
    require_once('../config.php');
    require_once('../'.DBAPI);
    require_once('../'.SESSIONAPI);

    if (isset($_POST['login'])) {
        $session = Session::getInstance();
        $db = open_database();

        /* Recuperação dos dados do login */
        $login = $_POST['login'];
        $pass = $_POST['pass'];
        $remember = $_POST['remember'];

        $sql = "SELECT * FROM Users WHERE login = '$login' AND pass = '$pass'";

        try {
            $result = $db->query($sql);
            $row = $result->fetch_assoc();
            if ($result->num_rows == 1) {
                $session->advocad = 'yes';
                $session->username = $row['username'];
                /* Criar os cookies */
                if ($remember == 'on') {
                    setcookie("login", $login, time() + (86400 * 30 * 12), "/"); /* nome_cookie, valor_cookie, duracao_cookie, acesso */
                    setcookie("pass", $pass, time() + (86400 * 30 * 12), "/");
                    setcookie("remember", $remember, time() + (86400 * 30 * 12), "/");

                } else {
                    setcookie("login", "", time() -3600, "/");
                    setcookie("pass", "", time() -3600, "/");
                    setcookie("remember", "", time() -3600, "/");
                    //var_dump($_COOKIE);
                }
                $auth = "OK";

            } else {
                $session->destroy();
                $auth = "NO";
            }

        } catch (Exception $e) {
            unset($_SESSION['advocad']);
            $auth = $e->GetMessage();
        }

        close_database($db);
        echo $auth;
    }
?>
  • If your problem is in window.location.replace() because for every PHP code, it has some direct relation?

  • 2

    I think what you really want is window.location.href = URL

  • I would also test the result that comes in data in response to the request AJAX with console.log(data). There may be some logic error in php code and it is not returning "OK".

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.