使用location.href将值传递给php脚本

时间:2014-05-04 14:56:11

标签: javascript php jquery

当用户点击注销按钮时,js调用会调用php脚本,该脚本将关闭会话并发送到主页。我正在尝试传递当前页面的url并将其存储在数据库中,因此当他们再次登录时,会将它们带到他们查看的最后一页,但我无法将值传递给php脚本。

js

  $(document).ready(function() {  
    $('#logout_btn').click(function(){  
      $.msgbox("Are you sure you want to log out?", {  
        type: "confirm", 
        buttons: [
          {type: "submit", value: "Yes"},
          {type: "cancel", value: "No"}
        ]
      }, function(result) {

        if (result == 'Yes') {
           var last_viewed = 'jim';
                 location.href = "logout.php?last_viewed="+last_viewed;
               }//close if yes 
            });//close function(result)
       });//close trigger
  });//close whole function

logout.php

session_start();
$_SESSION['user_id'];
$user_id = $_SESSION['user_id'];

$last_viewed = $_GET['last_viewed'];

include 'includes/db_connect.php';

$sql = "UPDATE user SET last_view = '$last_viewed' WHERE userID = '$user_id'";
$result = mysqli_query($mysqli,$sql) or die(mysqli_error($mysqli));

session_destroy();
header("location:index.php");
exit();

注销脚本,因为它没有从js文件中获取值last_viewed,因此没有值输入到数据库中。如果我在`$ last_viewed'中输入一个字面值正确输入值。

显然' jim'不是真正的网址,我只是用它来进行测试。

2 个答案:

答案 0 :(得分:1)

检查出来:

location.href = "logout.php?last_viewed="+encodeURIComponent(last_viewed);

答案 1 :(得分:0)

我移动了行

$last_viewed = $_GET['last_viewed'];

所以它低于

include 'includes/db_connect.php';

然后它奏效了。