会话变量未在javascript代码中更新 [英] Session variable does not get updated within javascript code
问题描述
delete_define.php具有以下代码段:
delete_define.php has the following code snippet:
<?php
session_start();
?>
<form action="delete_now.php" target="upload_target" onsubmit="return my_func_1();">
<input type="submit" name="my_submit" class="my_submit" value="submit"/>
<iframe id="upload_target" name="upload_target" src1111="#" style="width:0;height:0;border:0px solid #fff;"></iframe>
</form>
<script type="text/javascript">
function my_func_1(){
//alert("from within my_func() =" +<?php echo $_SESSION['my_session']; ?>);
alert(" my_func_1");
return true;
}
function my_func_2(){
alert("my_func_2 =" +<?php echo $_SESSION['my_session']; ?>);
return true;
}
</script>
delete_now.php具有:
delete_now.php has:
<?php
session_start();
$_SESSION['my_session']=rand();
?>
<script type="text/javascript">
alert("from within delete_now.php = " +<?php echo $_SESSION['my_session']; ?>);
window.top.window.my_func_2();
</script>
问题是my_func_2()不能为会话变量提供与delete_now.php中的警报框相同的输出.
The problem is my_func_2() does not give the same output for the session variable as the alert box in delete_now.php gives.
那是为什么?
EDIT: CHANGED THE CODE SAID TO BE IN delete_define.php
推荐答案
这是因为在delete_define.php加载时,会话var是一个,然后变成另一个,但是在JS中,您存储了以前的值.
That's because when the delete_define.php was loading the Session var was one, then it's become another, but in you JS stored previous value.
您应该将会话var存储到JS var中,然后在JS中的delete_now.php中使用新值将其重置.
You should store session var into JS var, and then in JS in delete_now.php reset it with the fresh value.
将类似以下内容的内容添加到第一个php文件的JS中:
Add to first php file's JS something like:
var session_var = '<?php echo $_SESSION['my_session']; ?>';
然后在您的delete_now.php的JS中:
And then in your delete_now.php's JS:
parent.session_var = '<?php echo $_SESSION['my_session']; ?>';
然后更改函数my_func来提醒session_var JS变量.
And change function my_func to alert session_var JS variable.
这样想...
说明:
然后结果页js将是:
function my_func_2(){
alert("my_func_2 = 13513513513513");
return true;
}
因此,当您调用它时,$ _ SESSION中的任何内容都会有旧的静态值.
So when you call it, whatever is in the $_SESSION is, there will be old, static value.
总体过程说明:
- 加载delete_define.php
- JavaScript var,包含已初始化的实际文件名
- 来自提交
- 脚本delete_now.php正在运行
- 主窗口中的Javascript变量刷新
- 您调用my_func_2(),它将使用包含新文件名的全局JS var.
这篇关于会话变量未在javascript代码中更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!