我在将会话变量从一个页面传递到另一个页面时遇到问题——只有一种情况。问题是,有时变量通过,有时不通过。大约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';
});
"patienttable"是人员列表。
"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...
- 我从来没有遇到过从B页移动到C页、从B页移到D页、从B页移到E页以及来回移动的问题。。。从来没有
- 问题总是从A页到B页
问题:
- 有人能给我指一个正确的方向来解决这个问题吗
- 你会设置这样的会话变量吗?还是更好的方法
我提前感谢你。
您在重定向到下一页之前没有完成文章。按如下方式更改代码。然后它会让你的帖子在重定向到下一页之前完成。
$("#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'
} );
;
});