将PHP变量发送给javascript函数 [英] Send PHP variable to javascript function

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

问题描述

我有一个php文件生成一个变量,我希望将这个变量放入一个javascript函数中,该函数由主页上的onclick调用。这是可能从PHP发送到JavaScript?

I have a php file that generates a variable and I would like the variable to be put into a javascript function which is called by onclick on the main page. Is this possible to send from PHP to javascript?

推荐答案

您可以执行以下操作:

You can do the following:

<script type='text/javascript'>
    document.body.onclick(function(){
        var myVariable = <?php echo(json_encode($myVariable)); ?>;
    };
</script>

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

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