0
I have a login form with PHP and JQUERY:
index php.:
<form action="." method="POST" class="form-signin">
<h1 class="h3 mb-3 font-weight-normal">Entrar</h1>
<label for="name" class="sr-only">name</label>
<input type="text" id="name" name="name" class="form-control" placeholder="Usuario...">
<label for="word" class="sr-only">Password</label>
<input type="password" id="word" name="password" class="form-control" placeholder="Senha...">
<button style="margin-top: 50px;" id="login" class="btn btn-style btn-lg btn-primary btn-block"><strong>Logar</strong></button>
<div style="margin-top: 35px;">
<div id="add_err" class="card">
</div>
</div>
</form>
login js.:
$(document).ready(function(){
$("#add_err").css('display', 'none', 'important');
$("#login").click(function(){
username=$("#name").val();
password=$("#word").val()
$.ajax({
type: "POST",
url: "login.php",
data: "name="+username+"&pwd="+password,
success: function(html){
if(html=='true') {
$("#add_err").html("right username or password");
//window.location="../index.php";
}
else {
$("#add_err").css('display', 'inline', 'important');
$("#add_err").html("<font style='margin-top: 50px; padding: 20px; background-color: #C70039; color: white; border-width: 0px;'>Usuario o Senha incorretos</font>");
$("#login").html('<strong>Logar</strong>');
}
},
beforeSend:function()
{
$("#login").html("<div class='spinner'><div class='bounce1'></div><div class='bounce2'></div><div class='bounce3'></div></div>");
$("#add_err").css('display', 'none', 'important');
$("#login").blur();
}
});
return false;
});
});
login.php:
sleep(3);
session_start();
//Incluindo a conexão com banco de dados
include_once("../assets/php/config.php");
$usuario = mysqli_real_escape_string($con, $_POST['name']); //Escapar de caracteres especiais, como aspas, prevenindo SQL injection
$senha = mysqli_real_escape_string($con, $_POST['pwd']);
$senha = md5($senha);
//Buscar na tabela usuario o usuário que corresponde com os dados digitado no formulário
$result_usuario = "SELECT * FROM admin WHERE name = '$usuario' && password = '$senha' LIMIT 1";
$resultado_usuario = mysqli_query($con, $result_usuario);
$resultado = mysqli_fetch_assoc($resultado_usuario);
//Encontrado um usuario na tabela usuário com os mesmos dados digitado no formulário
if(isset($resultado)){
echo "true";
$_SESSION['ID'] = $resultado['id'];
$_SESSION['NAME'] = $resultado['name'];
$_SESSION['PASSWORD'] = $resultado['password'];
$_SESSION['LEVEL'] = $resultado['level'];
}else{
echo "false";
}
The script does the query in the database and returns, if it is found it returns 'true'
and displays the success message, if not found it returns 'false'
and the error message, the problem is that regardless of the result of the query 'true'
or 'false'
it displays the error message!
And what would this error message be? Is this message not actually a warning message (Warning)? Because if it was a real error message, an unforeseen exception, your code would probably have stopped running even before printing "false";
– Andre
@user140828 The error message is the one that appears when the user or password are incorrect!
– FoX
Yes, but what would that message be? Is it generated by you or PHP? What is written on it? These points help to discover the reason for the error, but you have not posted where this message comes from, there is no way to help just knowing that somewhere there is an error message that we do not know how it is generated and what its content.
– Andre
@user140828 Oh, yes... It is generated in javascript (login.js). no if and Else (incorrect user or password)! I debug the script and it is said that he did the query and got the return, IE the code is not stopping before
– FoX
Make a
console.log(html);
beforeif(html=='true') {
and post here to see...– LipESprY
@Correct user and password Lipespry: console.log(HTML) = 'true'. Incorrect user and password console.log(HTML) = 'false'. working normally, I believe the problem is in AJAX but I’m not able to find!
– FoX
Bro. Seu
if/else
are working. I played, in part, your problem. I made a PHP page withecho 'true';
/echo 'false';
and AJAX worked perfectly. Even inif/else
. Is there any more code in there that might be influencing?– LipESprY
@Lipespry no other code! which version of jquery you used ? you can post your code so I can test ?
– FoX
Yes, I can! Later, if there is no solution here, put for you. I am in the cell now (away from the PC).
– LipESprY