My problem is that I always get the "failed" status, even though I put in the right values while logging on. However, when I modify my $sql
in the login.php code into right values from database, I get "success" status. I thought that maybe there is something wrong with sending data from AJAX to PHP. Could you please help me how to figure it out?
This is login.php code:
header('Access-Control-Allow-Origin: *');
include "database.php";
header("Content-Type: application/json");
$numer=$_POST['number'];
$pass=$_POST['pass'];
$sql="SELECT * FROM `Users` WHERE Phone = '$numer' AND Password = '$pass'";
if ($result=mysqli_query($db,$sql))
{
// Return the number of rows in result set
$rowcount=mysqli_num_rows($result);
if($rowcount==1)
{
while($row = $result->fetch_assoc()) {
$dane = array("status" => "success",
"ID" => $row["ID"],
"Name"=> $row["Name"],
"Surname"=> $row["Surname"],
"Phone"=> $row["Phone"],
"Password"=> $row["Password"],
"Admin"=> $row["Admin"],
"Class"=> $row["Class"],
"Banned"=> $row["Banned"]
);
}
echo json_encode($dane);
}
else
{
$dane = array("status"=>"failed");
echo json_encode($dane);
}
mysqli_free_result($result);
}
mysqli_close($db);
?>
This is the AJAX code:
$("#login").click(function(){
var number=$("#number").val();
var pass=$("#password").val();
if($.trim(number).length>0 & $.trim(pass).length>0)
{
$.ajax({
type: "POST",
url: "http://smietana.mechanikrac.website.pl/cgi-bin/janeta/login.php",
data: { number:number, pass:pass},
contentType:"application/json; charset=utf-8",
dataType:'json',
crossDomain: true,
cache: false,
beforeSend: function(){ $("#login").html('Connecting...');
alert(number+pass);},
success: function(data){
alert('test'+data["status"]);
if(data["status"]=="success")
{
localStorage.login="true";
localStorage.number=number;
alert(data["status"]);
window.location.href = "order.html";
$('#witaj').html(number);
}
else if(data["status"]=="failed")
{
alert("Błędne Dane");
$("#login").html('Login again');
}
}
});
}return false;
});