Validation does not redirect PHP / Jquery

Asked

Viewed 23 times

0

I have a validation form, but it is not redirecting to the page after checking the access when I use 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>";
   }
}

1 answer

1


Ideally you would return a flag in your method and treat the logic of redirecting on the frontend. Returning instead of your window.location.href, return true or false. In the post callback, next to console.log you check the answer and just 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;
    });
});
  • Perfect Rafael. Thank you very much!

Browser other questions tagged

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