-5
I made a login system with ajax and php in it I pass the login and the password that are in the database but I wanted to know what is wrong with my code because whenever I try to log into the site with a valid login it falls into my false condition and it appears that I can not redirect. gave an Alert on the return of ajax that shows the result of php page and is correct when I type the right data it brings me in an Alert that it was possible to connect what I need to do to my ajax not keep falling in false condition of my if? Obs will not post php because I tested only in php and it worked perfect 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);
Your php returns what after the query?
– rray
what comes out in the Alert(r)?
– Marconi
if or Else if it is right it returns and redirects to the page for example can eacessar.php and in Else it goes back to the same page in opodeacessar.php
– Leonardo Costa
in Alert is leaving the result of if php and Else
– Leonardo Costa
yes, but he didn’t print the Alert first(r)?
– Marconi
type the error and so if I soon right in Alert will appear login successfully but then it falls into that ajax Lse and will not
– Leonardo Costa
yes intao he prints that I just mentioned in Alert login successfully
– Leonardo Costa
more in if you compare
r == 1
, and the return of ré login efetuado com sucesso
?– Marconi
r
has what value?1
,0
,undefined
?– rray
depends if I put the data right it will show in the login Alert successfully done if I put the wrong data it will give that the user does not exist NO ALERT ai in this if theoretically it was for him to compare the result with truth type exister do this but it does not always makes falls in that if(r==1)
– Leonardo Costa
then you must have a different return to compare in IF. 1 for successfully performed and 0 for failed authentication, or something like
– Marconi
I’ve compared it with true tbm and always fell in Else even putting the values coretos
– Leonardo Costa
intact but that’s not what I’m doing I’m comparing it to 1 if there is going to such a page or back to msm
– Leonardo Costa
rray the r and return of the done ajax function
– Leonardo Costa
I’ll go up php code to see if it helps
– Leonardo Costa
ready edited to try to make it clearer here when I type everything right as what is registered in the database in this Alert(r) it returns me that <script type="text/javascript"> Alert("Login successfully!"); window.Location='.php'; </script>";
– Leonardo Costa
and when I type everything wrong it returns it to me in Alert(r) <script type="text/javascript"> Alert("User or password entered this incorrect!"); window.Location='. /admin/admin.php'; </script>"; but even if I type everything right with ajax it brings me the true ajax condition but the php one it brings me the login successfully
– Leonardo Costa