2

I have a PHP login script which executes with ajax. The ajax request now starts the session in the login successfully but the window.location function doesn't work (doesn't redirect to exporter.php) in the ajax request. Below are my codes.

php Login Script

if(isset($_POST['log_name']) && isset($_POST['log_password'])) {

    $username = $_POST['log_name'];
    $password = $_POST['log_password'];

    $sql = $db->prepare("SELECT * FROM users WHERE uname = ?");
    $sql->bindParam(1, $username, SQLITE3_TEXT);

    $ret = $sql->execute();

    while ($row = $ret->fetchArray(SQLITE3_ASSOC))
    {
        $id = $row['userid'];
        $regas = $row['regas'];
        $uemail = $row['uemail'];
        $pword = $row['pword'];
        $uname = $row['uname'];
        $package = $row['package'];

        if (password_verify($password, $pword))
        {


          $_SESSION['log_id'] = $id;
          $_SESSION['log_name'] = $username;
          $_SESSION['regas'] = $regas;
          $_SESSION['uemail'] = $uemail;
          $_SESSION['package'] = $package;
          //header("Location: index.php?log_id=$id");
          //echo "Sigining In...";
          //die("<script>window.location='exporter.php?userid={$id}';</script>");
          exit();
        }
        else
        {
            echo "Information incorrect";
            exit();
        }
    }
}

Ajax Request

$("#submit_log").click(function() {
      //e.preventDefault();
      username=$("#log_name").val();
      password=$("#log_password").val();
      $.ajax({
        type: "POST",
        url: "login.php",
        data: "log_name="+username+"&log_password="+password,
        success: function(html){
          if(html=='true')    {
            window.location.assign = "exporter.php";
          }
          else {
            $(".logresult").html('Incorrect Username and Password');
          }
        },
        beforeSend:function()
        {
          $(".logresult").html("Loading...")
        }
      });
      return false;
    });

Beginning part of exporter.php

session_start();
require_once ("db.php");
$db = new MyDB();

if (!isset($_SESSION['log_name']) || !isset($_SESSION['log_id']) || !isset($_SESSION['regas']))
{
    header("Location: index.php");
}

What could be wrong here and how do i fix this redirecting issue please!!!.Thanks.

1

5 Answers 5

2

your php login script needs echo 'true'; according to your ajax callback.

and use location.href = "/exporter.php"; to redirect page with JavaScript

Sign up to request clarification or add additional context in comments.

1 Comment

Perfect answer. Exactly what i was looking for. Thanks for pointing that out.
1

You should use like this:

window.location.href= "/exporter.php";

Comments

0
window.location.assign = "exporter.php";

will not work, use

window.location = "exporter.php";

Comments

0

You are using assign incorrectly.

window.location.assign("exporter.php")

Or you can use href instead.

window.location.href = "exporter.php";

Comments

0

Try using assign like this:

window.location.assign(data);

if this method not work for you let me know.

and if the Success:html is boolean then you're checking it in wrong way delete the single quotes it's not a string it is boolean datatype.

3 Comments

Neither method works. I am confused myself as where the issue is from.
please check that the if condition is being executed or not by using an alert() method.
@alen answer pointed out the issue with my php. Thanks for helping.

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.