How to check if the login exists in the bank by ajax?

-5

I made a login system with ajax and php in it I pass the login and the password that are in the bank but I wanted to know what is wrong with my code because whenever I try to login to the site with a valid login it falls on my false status and it appears that I can not redirect. I gave an alert in the ajax return that shows the result of the php page and this is correct when I type the correct data it brings me in an alert that it was possible to connect what I need to do for my ajax not to fall in the false condition of my if ? I'm not going to post php because I only tested in php and it worked perfectly the problem is in ajax

code:

$("#logar").click(function(){
          var email = $("#email").val(),
              senha = $("#senha").val();
          if(!senha){
            $('[data-toggle="tooltip"]').tooltip();
            $('#senha').tooltip('hide');
            $(".tooltip").show();
            $("#senha").focus();
            return;
          }

          $.ajax("../sys/logaradm.php",{
            type: "POST",
            data: {'usuario':email, 'senha':senha}
          }).done(function(r){
            alert(r);
            if(r==1){
            alertify.success("Acesso concedido redirecionando...");
            setTimeout("document.location = 'administracao.php'",2500);
          }else{
            alertify.error("Usúario ou senha informados nao existem!");
          }
          }).fail(function(){
            alertify.error("Ocorreu um erro durante a operacao!")
          }); 
      });

php code:

session_start();
    require "conexao.php";
    $usuario = trim(@$_POST['usuario']);

    $senha = trim(@$_POST['senha']);

    $sql_acesso = mysqli_query($mysqli, "SELECT * FROM adm WHERE email = '$usuario' AND senha = '$senha' ");
    if(mysqli_num_rows($sql_acesso) == 1 ){

        $_SESSION['usuarioSession'] = $usuario;
        $_SESSION['logadoadm']= true;
        //$_SESSION['senhaSession'] = $senha;


            echo "
                    <script type=\"text/javascript\">
                    alert(\"Login efetuado com sucesso!\");
                    window.location='administracao.php';
                    </script>";
        }
        else{
            echo "
                    <script type=\"text/javascript\">
                    alert(\"Usuario ou senha informado esta incorreto!\");
                    window.location='../admin/admin.php';
                    </script>";
        }
        mysqli_close($mysqli);
    
asked by anonymous 16.12.2015 / 00:02

1 answer

0

The problem is that the variable r of javascript never has the value 1 , because the text output of php is a string containing javascript.

view:

 echo "
     <script type=\"text/javascript\">
       alert(\"Login efetuado com sucesso!\");
       window.location='administracao.php';
       </script>";
 } else{
      echo "
      <script type=\"text/javascript\">
       alert(\"Usuario ou senha informado esta incorreto!\");
       window.location='../admin/admin.php';
       </script>";
}

Make php return the record number returned so, now r can have some numeric value like 0 or 1 . In javascript (client side) do the validations, alerts and redirects.

$existe_registro = mysqli_num_rows($sql_acesso);
if($existe_registro == 1 ){
    $_SESSION['usuarioSession'] = $usuario;
    $_SESSION['logadoadm']= true;
}
mysqli_close($mysqli);
echo $existe_registro;
    
16.12.2015 / 00:33