I want a handler for my login which will check if the username and password are in the database and if it's true it will redirect him to another page. I am trying to make this with JQuery Ajax and PHP
I made the JS Script that will send the inputs data of the form to my login-process.php file
//handling login form.
$("#login-form").submit(function(e){
if($('#user').val() != "" && $('#psw').val() != "" && $('#npsw').val() != "") {
$.ajax({
type: "POST",
url: "login-process.php",
data: $("#login-form").serialize(),
success: function(data){
$('#login-form')[0].reset();
console.log(data); // show response from the php script.
}
});
}
else {
alert("Please fill the fields.")
}
e.preventDefault();
});
and this is my login-process.php
<?php
require_once 'classes/user.php';
require_once 'classes/db.php';
function check(){
$name = $_POST['user'];
$psw = $_POST['psw'];
$db = new db();
$adminQuery = "SELECT* FROM admins";
$info = $db->getData($adminQuery); // My own method that fetches all the data given a query.
foreach ($info as $row){
if ($name == $row["nickname"] && sha1($psw) == $row["password"]) {
echo "True";
return true;
}
}
echo "false";
return false;
}
check();
?>
now my question is how do I check if the response is true or false and redirect to another page or inform the user for wrong password or username?
Do I use the success() function of the ajax object to make the handler redirect or inform for wrong username/password?.
success: function(response){ $('#login-form')[0].reset(); if (response == true) { window.location.href = "info.php"; }"True"console.log(result)?