I have this very simple login form with user and pass and I need to check that both are in a database and I’m using ajax for this:
$("#clientes").bind("submit", function() {
$(".error_usu").hide();
$.ajax({
type : "POST",
cache : false,
url : "login.php",
data : $(this).serializeArray(),
success: function(data) {
switch(data){
case '1'://exito
alert("alert!");
window.location.href = "clientes.php";
break;
case '2'://pass
$(".error_pass").show();
break;
}
}
});
return false;
});
inside login.php I have this:
<? session_start();
if(!empty($_POST['usuario']) && !empty($_POST['password']))
{
include "funciones.php";
$username = $_POST['usuario'];
$password = $_POST['password'];
$con=conectar();
$checklogin = mysql_query("SELECT * FROM clientes WHERE user = '".$username."' AND pass = '".$password."'",$con) or die (mysql_error());
if(mysql_num_rows($checklogin) == 1){
$_SESSION['usuario'] = $_POST['usuario'];
$_SESSION['password'] = $_POST['password'];
echo '1';
}else{
echo '2';
}
}else{
echo '2';
}?>
When I check with firebug I see I’m getting a 1 or a 2 but de success function is never working! I tried this before I had the database just checking this way inside login.php and it worked:
if($_POST['usuario']=="fulano" && $_POST['password']=="fulano"){
echo '1';
}
Is it not working because of the $_SESSION variables? I don’t fully understand how ajax function works.
If it’s not entering the switch statement you could multiply the response with 1, then JavaScript makes it a number instead of text.
or