将PHP变量传递给Javascript的最佳方法是什么? [重复]

时间:2022-01-12 00:10:26

This question already has an answer here:

这个问题在这里已有答案:

I currently echo certain variables in hidden input fields and read them out with Javascript whenever I need them.

我目前在隐藏的输入字段中回显某些变量,并在需要时用Javascript读出它们。

Me and a colleague are now thinking of generating an extra Javascript file with PHP which only contains all variables for Javascript. This way the variables already exist and there is no extra code in the HTML.

我和一位同事正在考虑使用PHP生成一个额外的Javascript文件,该文件仅包含Javascript的所有变量。这种方式变量已经存在,HTML中没有额外的代码。

What are good ways to pass variables from PHP to Javascript? And how does our solution sound?

将变量从PHP传递到Javascript的好方法是什么?我们的解决方案听起来如何?

4 个解决方案

#1


25  

General Data Passing

A commonly used exchange format for JavaScript is JSON, using json_encode. A PHP file like this:

JavaScript的常用交换格式是JSON,使用json_encode。像这样的PHP文件:

<?php
    $data = array("test" => "var", "intvalue" => 1);
    echo json_encode($data);
?>

then returns a JavaScript object literal like this:

然后返回一个JavaScript对象文字,如下所示:

{
    "test" : "var",
    "intvalue" : 1
}

You can directly echo it into a JavaScript variable on your page, e.g.:

您可以直接将其回显到页面上的JavaScript变量,例如:

var data = <?php echo json_encode($data)?>;

...or request it via Ajax (e.g. using jQuery's getJSON).

...或通过Ajax请求它(例如使用jQuery的getJSON)。

Outputting to attributes on tags

If you just need to output a string to an attribute on a tag, use htmlspecialchars. Assuming a variable:

如果只需要将字符串输出到标记上的属性,请使用htmlspecialchars。假设一个变量:

<?php
$nifty = "I'm the nifty attribute value with both \"double\" and 'single' quotes in it.";
?>

...you can output it like this:

...你可以像这样输出:

<div data-nifty-attr="<?php echo htmlspecialchars($nifty)?>">...</div>

...or if you use short tags:

...或者如果您使用短标签:

<div data-nifty-attr="<?= htmlspecialchars($nifty)?>">...</div>

#2


5  

<?php
  $my_php_var = array(..... big, complex structure.....);
?>
<script type="text/javascript">
  my_js_var = <?=json_encode ($my_php_var)?>;
</script>

#3


3  

I use to echo them all together at the head of the HTML. Seems clean enough for my needs :)

我用它在HTML的头部回显它们。似乎足够干净我的需要:)

#4


2  

There are 3 ways you could do it:

有三种方法可以做到:

  • Echo them directly into the javascript source: <?echo "var user = '$user';";?>. Works, but it's messy.
  • 将它们直接回显到javascript源: 。工作,但它很乱。

  • Pass them in via an ajax request. This is the closest you can come to native variable passing, but the downside is it takes an extra HTTP request.
  • 通过ajax请求传递它们。这是您可以接近原生变量传递的最接近的,但缺点是需要额外的HTTP请求。

  • What you're doing, which is passing them by generating hidden form fields and then reading them.
  • 你在做什么,通过生成隐藏的表单字段然后阅读它们来传递它们。

#1


25  

General Data Passing

A commonly used exchange format for JavaScript is JSON, using json_encode. A PHP file like this:

JavaScript的常用交换格式是JSON,使用json_encode。像这样的PHP文件:

<?php
    $data = array("test" => "var", "intvalue" => 1);
    echo json_encode($data);
?>

then returns a JavaScript object literal like this:

然后返回一个JavaScript对象文字,如下所示:

{
    "test" : "var",
    "intvalue" : 1
}

You can directly echo it into a JavaScript variable on your page, e.g.:

您可以直接将其回显到页面上的JavaScript变量,例如:

var data = <?php echo json_encode($data)?>;

...or request it via Ajax (e.g. using jQuery's getJSON).

...或通过Ajax请求它(例如使用jQuery的getJSON)。

Outputting to attributes on tags

If you just need to output a string to an attribute on a tag, use htmlspecialchars. Assuming a variable:

如果只需要将字符串输出到标记上的属性,请使用htmlspecialchars。假设一个变量:

<?php
$nifty = "I'm the nifty attribute value with both \"double\" and 'single' quotes in it.";
?>

...you can output it like this:

...你可以像这样输出:

<div data-nifty-attr="<?php echo htmlspecialchars($nifty)?>">...</div>

...or if you use short tags:

...或者如果您使用短标签:

<div data-nifty-attr="<?= htmlspecialchars($nifty)?>">...</div>

#2


5  

<?php
  $my_php_var = array(..... big, complex structure.....);
?>
<script type="text/javascript">
  my_js_var = <?=json_encode ($my_php_var)?>;
</script>

#3


3  

I use to echo them all together at the head of the HTML. Seems clean enough for my needs :)

我用它在HTML的头部回显它们。似乎足够干净我的需要:)

#4


2  

There are 3 ways you could do it:

有三种方法可以做到:

  • Echo them directly into the javascript source: <?echo "var user = '$user';";?>. Works, but it's messy.
  • 将它们直接回显到javascript源: 。工作,但它很乱。

  • Pass them in via an ajax request. This is the closest you can come to native variable passing, but the downside is it takes an extra HTTP request.
  • 通过ajax请求传递它们。这是您可以接近原生变量传递的最接近的,但缺点是需要额外的HTTP请求。

  • What you're doing, which is passing them by generating hidden form fields and then reading them.
  • 你在做什么,通过生成隐藏的表单字段然后阅读它们来传递它们。