Ajax window.location无法使用PHP登录脚本

时间:2017-08-01 20:57:01

标签: javascript php jquery ajax

我有一个使用ajax执行的PHP登录脚本。 ajax请求现在成功启动登录中的session,但window.location函数在ajax请求中不起作用(不会重定向到exporter.php)。以下是我的代码。

php登录脚本

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请求

$("#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;
    });

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");
}

这里可能有什么问题,如何解决这个重定向问题!!!。谢谢。

5 个答案:

答案 0 :(得分:2)

根据你的ajax回调,你的php登录脚本需要echo 'true';

并使用location.href = "/exporter.php";重定向带有JavaScript的页面

答案 1 :(得分:1)

你应该这样使用:

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

答案 2 :(得分:0)

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

无效,请使用

window.location = "exporter.php";

答案 3 :(得分:0)

您使用的分配不正确。

<?php class Booking { private $minGuests = 1; protected $guests = 1; protected $rooms = []; public function getRoomCombinations(bool $exact = true) { $guests = $this->guests; $list = []; $rooms = $this->rooms; /*for($i = 0; $i < $guests-1; $i++) { $rooms = array_merge($rooms, $this->rooms); } asort($rooms);*/ $this->extractList($rooms, $list); $result = array_filter($list, function($var) use ($guests, $exact) { if($exact) return(array_sum(array_map(function($item) { return $item['room_for'];}, $var)) == $guests); else return(array_sum(array_map(function($item) { return $item['room_for'];}, $var)) >= $guests && count($var) <= $guests); }); array_multisort(array_map('count', $result), SORT_ASC, $result); return $result; } private function extractList(array $array, array &$list, array $temp = []) { if (count($temp) > 0 && !in_array($temp, $list)) $list[] = $temp; for($i = 0; $i < sizeof($array); $i++) { $copy = $array; $elem = array_splice($copy, $i, 1); if (sizeof($copy) > 0) { $add = array_merge($temp, array($elem[0])); sort($add); $this->extractList($copy, $list, $add); } else { $add = array_merge($temp, array($elem[0])); sort($add); if (!in_array($temp, $list)) { $list[] = $add; } } } } public function setGuests(int $guests) { $this->guests = ($guests >= $this->minGuests ? $guests : $this->minGuests); return $this; } public function setHotelRooms(array $rooms) { $this->rooms = $rooms; return $this; } } $booking = (new Booking()) ->setGuests(4) ->setHotelRooms([ [ "title" => "1 person room", "room_for" => 1, "price" => 79 ], [ "title" => "2 person room with other", "room_for" => 1, "price" => 69 ], [ "title" => "2 person room alone", "room_for" => 1, "price" => 89 ], [ "title" => "2 person", "room_for" => 2, "price" => 69 ], [ "title" => "3 person", "room_for" => 3, "price" => 69 ] ]); echo '<pre>' . var_export($booking->getRoomCombinations(true), true) . '</pre>'; ?>

或者你可以改用href。

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

答案 4 :(得分:0)

尝试使用这样的分配:

window.location.assign(data);

如果这种方法不起作用,请告诉我。

如果Success:html是布尔值,那么你是以错误的方式检查它删除单引号它不是字符串它是布尔数据类型。