将 PHP 变量发送到 javascript 函数

2022-01-04 00:00:00 post get 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?

推荐答案

您可以执行以下操作:

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

相关文章