如何从.js文件中的jQuery函数访问PHP会话变量? [英] How to access PHP session variables from jQuery function in a .js file?

查看:109
本文介绍了如何从.js文件中的jQuery函数访问PHP会话变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从.js文件中的jQuery函数访问PHP会话变量?
在此代码中,我想从会话变量中获取值

How to access PHP session variables from jQuery function in a .js file? In this code, I want to get "value" from a session variable

$(function() {
   $("#progressbar").progressbar({
      value: 37
   });
});


推荐答案

您可以通过PHP生成javascript文件。没有什么说javascript文件必须有.js扩展名。例如,在您的HTML中:

You can produce the javascript file via PHP. Nothing says a javascript file must have a .js extention. For example in your HTML:

<script src='javascript.php'></script>

然后你的脚本文件:

<?php header("Content-type: application/javascript"); ?>

$(function() {
    $( "#progressbar" ).progressbar({
        value: <?php echo $_SESSION['value'] ?>
    });

    // ... more javascript ...

如果这个特殊方法不是一个选项,你可以在你的javascript文件中放入一个AJAX请求,并从服务器端脚本中将数据作为JSON返回。

If this particular method isn't an option, you could put an AJAX request in your javascript file, and have the data returned as JSON from the server side script.

这篇关于如何从.js文件中的jQuery函数访问PHP会话变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆