将 PHP 变量传递给 Javascript/jquery

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8758187/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 04:29:38  来源:igfitidea点击:

Pass PHP variables to Javascript/jquery

phpjavascriptjquery

提问by viper

So far I know two ways to pass php variables to javascript. One is by using

到目前为止,我知道两种将 php 变量传递给 javascript 的方法。一种是通过使用

<script>  
      $(document).ready(function()
          phpvalue=$("#hiddeninput").val()

      })
  </script>

<input type="hidden" id="hiddeninput" value="phpvalue">

And the other one is by having for example a button and using onclick

另一种是通过例如一个按钮并使用 onclick

    <script>
       function clickf(phpvalue) {

       }
    </script>

<input type="submit" onclick="clickf(<?php echo phpvalue ?>)">

All of them work great but:

它们都很好用,但是:

  1. Is there any other way that I'm missing?

  2. Which one is the "best"?

  3. Any chance that I can use inside the script or the external js ?

  1. 有没有其他方法让我失踪?

  2. 哪一个是最好的”?

  3. 我有机会在脚本内部或外部 js 中使用吗?

回答by Alex Pliutau

<script>  
    $(document).ready(function()
        var phpvalue = <?php echo $var; ?>
    });
</script>

回答by Rudolph Gottesheim

Like others already answered, just put a PHP tag whereever you would place the JS value, like var foo = <?= $php_value ?>;or var foo = <?php echo $php_value ?>;.

就像其他人已经回答过的那样,只需在要放置 JS 值的任何位置放置一个 PHP 标记,例如var foo = <?= $php_value ?>;var foo = <?php echo $php_value ?>;

If you want to use this in an external JavaScript file, you have to make sure .js files get parsed by PHP. If this is not an option for you (for example, your host doesn't allow it), I suggest you set the values in a <script>tag inside your <head>and then just reference thos variables from your external JavaScript. In that case, I would strongly suggest you namespace them like var app_vars = { foo: <?= $bar ?> }in order to not clutter the global object.

如果你想在外部 JavaScript 文件中使用它,你必须确保 .js 文件被 PHP 解析。如果这不是您的选择(例如,您的主机不允许),我建议您在<script>内部标签中设置值<head>,然后从外部 JavaScript 中引用这些变量。在这种情况下,我强烈建议您像命名它们一样命名它们var app_vars = { foo: <?= $bar ?> },以免弄乱全局对象。

Another way would be to retreive the values via Ajax. But the viability of this approach depends on your use case.

另一种方法是通过 Ajax 检索值。但是这种方法的可行性取决于您的用例。

And another hint: if you want to pass multiple variables or arrays, there's JSON baked into PHP since version 5.2:

另一个提示:如果您想传递多个变量或数组,自 5.2 版以来,PHP 已内置 JSON:

<?php
    $my_complex_var = array(
        'array' => array('foo', 'bar'),
        'val2' => 'hello world'
    );
?>
<script>
    var my_complex_js_var = <?= json_encode($my_complex_var) ?>
</script>

回答by Joeri

<script>

var javascript_variable = <?php echo $php_variable; ?>;

</script>

回答by nikc.org

Instead of assigning values to hidden inputs, you could just generate JavaScript directly:

您可以直接生成 JavaScript,而不是为隐藏输入分配值:

$script = <<<EOT
<script>
var phpvalue = $phpvalue;
</script>
EOT;
echo $script;

回答by Simon

for the top example you do not need to use val you could use any attr you like. For example

对于最上面的例子,你不需要使用 val 你可以使用任何你喜欢的 attr。例如

phpvalue="myvalue"

then within jquery

然后在 jquery 中

$("#hiddeninput").attr("phpvalue");