1
I am making a login screen with Ajax, jQuery and PHP.
Follows the code:
$(document).ready(function(){
//Quando 'btnEntrar' for clicado
$("#btnEntrar").click(function(){
//Envia por POST para a página login.php: usuario = valor da textbox usuario
//e senha = valor da textbox senha (pegando valores pelo ID)
var envio = $.post("processarLogin.php", {
nick: $("#form-username").val(),
senha: $("#form-password").val()
})
$.ajax({
beforeSend: function(){
$("#resultado").html("<div class='alert alert-info'><i class='fa fa-spinner'></i> Verificando dados...</div>");
}
})
//Se achou a página, exiba o resultado no elemento com ID resultado
envio.done(function(data) {
if(data==0){
$("#resultado").html("<div class='alert alert-danger'><i class='fa fa-warning'></i> Nome de usuário ou senha incorretos.</div>");
}else{
$("#resultado").html("<div class='alert alert-success'><i class='fa fa-check-circle'></i> Login bem sucedido, você será redirecionado...</div>");
setTimeout(function(){ document.location.href='painel.php';},500);
}
})
//Se envio falhar
envio.fail(function() { alert("Erro na requisição"); })
});
});
As you can see, I use the $.post to send the request. However, I used the $.ajax only to use the beforeSend, and works, displays the message before, during and after validation...
Is that wrong? What would the request look like if it were all done with $.ajax?