0

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?.

10
  • Well you are already console.logging the data so why not use an if statement there to see if the response is true and to redirect the user or to inform the user of a bad login. Commented Mar 12, 2017 at 1:50
  • do I check this in the success function ? success: function(response){ $('#login-form')[0].reset(); if (response == true) { window.location.href = "info.php"; } Commented Mar 12, 2017 at 1:56
  • yes, although you're going to need to capitalize the t and put it all in quotes like this: "True" Commented Mar 12, 2017 at 1:57
  • well it's nor working sir. Commented Mar 12, 2017 at 2:10
  • What is the output you get in the console from console.log(result)? Commented Mar 12, 2017 at 2:11

1 Answer 1

0

Youcan check you ajax success dats and redirect to page like below.

success: function(data){
 if(data. == true){
 window.location.href="success page name";
 }else{
 window.location.href="faile page name";
 }
 }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.