如何将 PHP 会话变量传递给 Javascript/jQuery 函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8618163/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to pass PHP session variable into Javascript/jQuery function?
提问by Michael
I have a PHP page that loads a session variable:
我有一个加载会话变量的 PHP 页面:
$user_id = $_SESSION['USER_ID'];
Previously, I included my Javascript/jQuery within that page, and added <? echo $user_id; ?>
to set the Javascript variable:
以前,我在该页面中包含了我的 Javascript/jQuery,并添加<? echo $user_id; ?>
以设置 Javascript 变量:
$(document).ready(function() {
$(".button").click(function() {
var user_id = <? echo $user_id; ?>
var dataString = 'user_id=' + user_id;
$.ajax({
type: "POST",
url: "../add_user.php",
data: dataString,
});
return false
});
});
However, I'd like to move my Javascript to a separate page and call the script from my PHP page:
但是,我想将我的 Javascript 移动到一个单独的页面并从我的 PHP 页面调用脚本:
<script src="add_user.js" type="text/javascript"></script>
If I do this, I can no longer user <? echo $user_id; ?>
, so what is the best way to pass my PHP variable into the Javascript/jQuery function?
如果我这样做,我就不能再使用 user <? echo $user_id; ?>
,那么将我的 PHP 变量传递给 Javascript/jQuery 函数的最佳方法是什么?
回答by Marc B
You can configure your webserver to treat .js files as PHP scripts, which would let you execute your PHP code from within the .js file.
您可以将网络服务器配置为将 .js 文件视为 PHP 脚本,这样您就可以从 .js 文件中执行 PHP 代码。
However, if that's not acceptable, you can always do something like:
但是,如果这是不可接受的,您始终可以执行以下操作:
<script type="text/javascript">var user_id = <?php echo json_encode($user_id) ?>;</script>
<script type="text/javascript" src="add_user.js"></script>
to define the variable at your PHP script level, and then include the external .js as usual.
在 PHP 脚本级别定义变量,然后像往常一样包含外部 .js。
If $user_id is always numeric, then the json_encode() bit is overkill, but I've gotten into the habit of using that everywhere I'm generating JS code dynamically. Using json_encode guarantees you're inserting a syntactically correct JS snippet.
如果 $user_id 总是数字,那么 json_encode() 位就有点矫枉过正了,但我已经养成了在动态生成 JS 代码的任何地方都使用它的习惯。使用 json_encode 可确保您插入语法正确的 JS 代码段。
回答by Aaron W.
<script>
var user_id = <?php echo $user_id; ?>
</script>
<script src="add_user.js" type="text/javascript"></script>
Now you can use user_id
in your add_user.js
file. Also, I would advise against using PHP short tags.
现在您可以user_id
在您的add_user.js
文件中使用。另外,我建议不要使用 PHP 短标签。
回答by Dominic Green
You will still be able to reference your varible in your external js file
您仍然可以在外部 js 文件中引用您的变量
<script type="text/javascript">
var yourvar = <? echo $user_id; ?>;
</script>
<script src="add_user.js" type="text/javascript"></script>
回答by Rolice
Just start the session on the other side. Its is also more secured, considering that JS data may be corrupted, even deliberately and may be exploited (security).
只需在另一侧开始会话。它也更安全,考虑到 JS 数据可能被破坏,甚至是故意的,可能会被利用(安全性)。
After starting the session - read the value there.
开始会话后 - 读取那里的值。
Of course, this is valid for scripts on the same vhost.
当然,这对同一虚拟主机上的脚本有效。