我还不是 JSON/AJAX 高手,所以我不知道该怎么做。
我需要一个 $_SESSION['name'] PHP 变量来处理我的 jQuery 内容,但我不知道如何访问它...考虑:
// the 'who is typing' shindig
$.ajax(
{
url: "whos_typing.html",
cache: false,
success: function(whos)
{
// here I need to access $_SESSION['name'] and do stuff with it
$("#soandso").html(whos); //Insert who's typing into the #soandso
}
});
最佳答案
你需要注入(inject)它,像这样:
var sessName = '<?php echo $_SESSION['name']?>';
包含此脚本的文件必须由 php 解释器执行(即 .php 文件)
编辑:同意 Radu 的观点,对未清理的数据执行会更安全:
var sessName = <?php echo json_encode($_SESSION['name']) ?>;
关于php - 从 Jquery 获取 PHP $_SESSION 变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11911779/