Validation does not redirect PHP / JQuery

0

I have a validation form, however it is not redirecting to the page after checking access when using JQuery, but when I use it directly in PHP, it works.

JQuery

$(document).ready(function(){
   $('#login-form').submit(function() {
     data = $('#login-form').serialize();

     $.post("validar.php",{
            d: data,
     },
      function (d) {
        console.log(d);
       if(d == 1){
           $('#myModal').modal('show');
        }
      });
      return false;
    });
});

PHP

$params = array();
$dados = parse_str($_POST['d'], $dado);
$mensagem = $metodos->validaUsuarios($dado["TipoAcesso"],$dado["LoginAcesso"],$dado["SenhaAcesso"]);
echo $mensagem;

PHP Method

 public function validaUsuarios($tipoAcesso,$loginUsuario,$senhaUsuario){    
....
  $sqlAcessos = mysqli_query($this->conexao,"SELECT * FROM acesso_usuarios WHERE Matricula = '%s' AND SenhaAcessos = '%s' AND TipoAcessos = '%s'",mysqli_real_escape_string($this->conexao,$loginUsuario),mysqli_real_escape_string($this->conexao,$codificado),mysqli_real_escape_string($this->conexao,$acesso));

if($contarAcessos == 0){
           $erro = 1;
       }else{
   .....
return "<script>window.location.href='sistema/';</script>";
   }
}
    
asked by anonymous 05.07.2017 / 16:21

1 answer

1

Ideally, you should return a flag in your method and treat the logic of the frontend. Returning instead of your window.location.href, return true or false . In the post callback, next to console.log you check the response and only add the window.location.href='sistema/';

PHP Function:

public function validaUsuarios($tipoAcesso,$loginUsuario,$senhaUsuario){    
    $sqlAcessos = mysqli_query($this->conexao,"SELECT * FROM acesso_usuarios WHERE Matricula = '%s' AND SenhaAcessos = '%s' AND TipoAcessos = '%s'",mysqli_real_escape_string($this->conexao,$loginUsuario),mysqli_real_escape_string($this->conexao,$codificado),mysqli_real_escape_string($this->conexao,$acesso));
    if($contarAcessos == 0){
       return 1;
    }else{
       return 0;
    }
}

JS:

$(document).ready(function() {
    $('#login-form').submit(function() {
        var data = $('#login-form').serialize();
        var post = {
            d: data,
        };
        var callback = function(d) {

            console.log(d);

            if (d == 1) {
                $('#myModal').modal('show');
            } else {
                window.location.href = 'sistema/';

            }
        };
        $.post("validar.php", post, callback);
        return false;
    });
});
    
05.07.2017 / 16:37