Can't login with ajax returns the login page as success.
This is the ajax code.
<script>
$(document).ready(function(){
$('#signIn').click(function() {
var user = $('#user').val();
var pass = $('#password').val();
if(user == '' || pass ==''){
alert("Please Fill All Fields");
}else{
$.ajax({
type: "POST",
url: "functions.php",
data: {user: user, pass: pass},
cache: false,
success: function(result){
//This returns the login page itself.
console.log(result);
}
});
}
return false;
});
});
</script>
Php file for login
<?php
include_once "../private/classes/login.class.php";
$userLogin = new Login($user,$pass);
if($_SERVER['REQUEST_METHOD']=='POST'){
$user = $_POST['user'];
$pass =$_POST['pass'];
$userLogin->signIn();
}
?>
Php Class
<?php
include_once "db.class.php";
class Login extends Dbh{
public $user;
public $pass;
public function __construct($user, $pass){
$user = $this->user;
$pass = $this->pass;
}
public function signIn(){
$sql = "SELECT username, email, password, role FROM admin Where (email = '$user' OR username = '$user')
UNION
SELECT username, email, password, role FROM employee Where (email = '$user' OR username = '$user' )
UNION
SELECT username, email, password, role FROM customer Where (email = '$user' OR username = '$user')";
$result = $this->connect()->query($sql);
$numRows = $result->num_rows;
if($numRows > 0){
$row = $result->fetch_object();
$db_pass = $row->password;
$role = $row->role;
// I entered password manually using phpmyadmin so it not hashed. This is used
//to hash the db password for checking purpose.
$hashed_password = password_hash($db_pass, PASSWORD_DEFAULT);
if(password_verify($pass,$hashed_password)){
return true;
}else{
header('location:login.php');
}
}else{
header('location:login.php');
}
}
}
?>
Normal login works fine but can't use ajax login. Tried different possible solutions from stackoverflow and other sites, But didn't work. I think ajax is not sending any request to functions.php.