使用location.href传递值给PHP脚本

pass value to php script using location.href

本文关键字:PHP 脚本 location href 使用      更新时间:2023-09-26

当用户单击注销按钮时,一个php脚本被一个js调用调用,该调用关闭会话并将其发送到主页。我试图传递当前页面的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,所以没有值输入到db中。如果我在' $last_viewed'中输入一个文字值,则输入的值是正确的。

显然'jim'不是真正的url,我只是用它来测试。

查看:

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

我移动了这一行

$last_viewed = $_GET['last_viewed'];

所以在

下面

include 'includes/db_connect.php';