页面之间会话变量的传递不一致

时间:2013-06-28 14:16:09

标签: php session-variables

我在将会话变量从一个页面传递到另一个页面时遇到问题 - 仅在一种情况下。 问题是有时变量通过,有时不通过。 大约50%的时间我需要刷新第二页(多次)才能正确读取变量。 当它不读取“新”变量时,使用“旧”会话变量并引出不同的人。 我并没有“丢失”变量,只是页面没有读取“新”变量。

这是布局:

.............................Page A
...............................|
...............................|
.............................Page B
...............................|
.............__________________|___________________
.............|.................|..................|
___________Page C...........Page D..............Page E

页面A是从数据库中获取的行的人员列表。 当您单击某行时,它会“选择”具有此代码的人员:

$("#patienttable tr").click(function() {
       var passthis = $(this).find("#localid").html();
       $.post("php/setsessionvariable.php",
                                       {sessionval: passthis}      );
       window.location.href = 'http://www.xxxxxxxx.xxx/xxxxxx/xx/xxxx.php';
});

“耐心”是人名单。

“passthis”是人物标识符(来自“localid”)。

“setsessionvariable.php”是设置变量的服务器端php文件。

这是设置会话变量的php文件(非常无聊,呵呵?)

<?php
session_start();
$_SESSION['patientidentifier'] = $_POST['sessionval'];
?>

在页面B,C,D和E中,会话变量由与HTML相同的页面上的php代码读取,如下所示; 这些是文件顶部的第一行。

<?php
session_start();
$ptidentifier = $_SESSION['patientidentifier'];
...rest of php code to pull data from the db and post it on the page...
...html code...
...javascript code...
...end of page...
  1. 我从来没有遇到过从第B页到第C页,第B页到第D页,第B页到第E页,以及来回......从来都没有问题。
  2. 问题始终是从第A页到第B页。
  3. 问题:

    1. 有人能指出我正确的方向来解决这个问题吗?
    2. 你会设置这样的会话变量吗?或者更好的方式?
    3. 我提前感谢你。

1 个答案:

答案 0 :(得分:0)

在重定向到下一页之前,您没有完成帖子。更改您的代码如下。然后它会允许你的帖子在重定向到下一页之前完成。

$("#patienttable tr").click(function() {
       var passthis = $(this).find("#localid").html();
       $.post("php/setsessionvariable.php",
                                       {sessionval: passthis} , function (e) {
window.location.href = 'http://www.thedentons.us/easyehr/v2/root.php'
}     );
       ;
});