[英]how to get php session value in javascript file
i have a php file in which i am calling external takeatour.js file in that js file i want php session data.by using a code.... 我有一个php文件,其中我在该js文件中调用外部takeatour.js文件,我希望使用代码来实现php会话data ....
$(this).ready(function() {
var category = <?php echo json_encode($this->session->userdata('category')); ?>;
alert("category="+category);
});
is it correct?? 这是正确的吗?? plz give me some suggestion.
请给我一些建议。 Thanks!!
谢谢!!
You can pass the session variable from PHP code to javascript code like this: 您可以将会话变量从PHP代码传递到javascript代码,如下所示:
<?php $variablephp = $_SESSION['category']; ?> <script> var variablejs = "<?php echo $variablephp; ?>" ; alert("category = " + variablejs); </script>
Add .php
to the end of the javascript file. 将
.php
添加到javascript文件的末尾。 That way the server will process it. 这样服务器将对其进行处理。
If you include it in a page that a client will download later, make sure to type=text/javascript"
in your script tag because it won't be happy with the extension otherwise. 如果您将其包含在客户端稍后将下载的页面中,请确保在脚本标签中
type=text/javascript"
,因为否则它将对扩展名不满意。
Whatever editor you use will likely not syntax highlight properly since you will not be following php conventions. 无论您使用哪种编辑器,都不会正确地突出显示语法,因为您将不会遵循php约定。
you can pass the category value as a parameter in the src attribute. 您可以在src属性中将类别值作为参数传递。 For example:
例如:
<script src="myjs.js?category=<?php echo $this->session->userdata('category'); ?>"></script>
and then get the category value inside your script. 然后在脚本中获取类别值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.