将变量从php传递给javascript [英] passing variables from php to javascript

查看:104
本文介绍了将变量从php传递给javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个网站,有人可以创建一个项目,数据库将存储一个id,php会为该id生成一个url。因此,下次当该人返回该网址时,它将记住该人的设置(变量)。现在的问题是,在我的网站中,javascript需要知道这些变量。

I am trying to create a site where someone can create an "item" and the database will store an id and php generates a url for that id. So next time when the person comes back with that url it will remember the person's settings (variables). Now the problem is that in my site javascript need to know these variables.

那么最佳解决方案是什么?传递超全球GET中的变量或者cookie?或者有更好的方法将这些变量传递给javascript吗?

So what is the best solution for this? passing the variables in the superglobal "GET" or maybe cookies? Or is there a better way to pass these variables to javascript?

推荐答案

只需使用php打印一些动态javascript

just use php to print some dynamic javascript

<script>
var myVar = "<?php echo json_encode($_COOKIE['somevalue']);?>";
</script>

这篇关于将变量从php传递给javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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