简体   繁体   English

如何将变量从 PHP 发送到 Javascript?

[英]How can I send variables from PHP to Javascript?

I'm a newbie with JavaScript, and I can't make this codes work.我是 JavaScript 的新手,我无法使这些代码工作。 I just want to know if I can put the JavaScript code inside an HTML code or it should be a different file.我只想知道我是否可以将 JavaScript 代码放在 HTML 代码中,或者它应该是一个不同的文件。

<?php
$bool = false;
$num = 3 + 4;
$str = "A string here";
?>

<script type="text/javascript">
// boolean outputs "" if false, "1" if true
var bool = "<?php echo $bool ?>"; 

// numeric value, both with and without quotes
var num = <?php echo $num ?>; // 7
var str_num = "<?php echo $num ?>"; // "7" (a string)
var str = "<?php echo $str ?>"; // "A string here"
</script>

将值从PHP传递到JavaScript ,您应该使用json_encode()函数。

var bool = <?= json_encode($bool); ?>

You can pass the PHP variable to JavaScript using the php tags which is included into the js functions.您可以使用包含在 js 函数中的 php 标签将 PHP 变量传递给 JavaScript。

Necessary Checks:必要的检查:

  1. Ensure that you place the ;确保您放置了; at the end of the line.在行尾。
  2. You need to echo the variable in order to set the value..您需要echo显变量以设置值..

Example:例子:

<?php
$date= date();
?>
<script>
    document.write(<?php echo $date; ?>);
</script>

The same trick can be applied to other data types (eg integers, arrays, objects, etc.).相同的技巧可以应用于其他数据类型(例如整数、数组、对象等)。 The following passes an entire array from PHP to JavaScript:下面将整个数组从 PHP 传递到 JavaScript:

Using json_encode() , you'll always get a properly formatted JavaScript object.使用json_encode() ,您将始终获得格式正确的 JavaScript 对象。

$shirt = array(
    'color' => 'blue',
    'number' => 23,
    'size' => 'XL'
);

echo '<script>';
echo 'var shirt = ' . json_encode($shirt) . ';';
echo '</script>';

The output looks like this:输出如下所示:

<script>var shirt = {"color":"blue","number":23,"size":"XL"}</script>

To pass scalar data held in a PHP variable ($val) to a JavaScript variable, place the following in a JavaScript segment:要将保存在 PHP 变量($val)中的标量数据传递给 JavaScript 变量,请将以下内容放在 JavaScript 段中:

var val = "<?php echo $val ?>";

Notice the quotes around the PHP tags.注意 PHP 标签周围的引号。 This will result in a string value in JavaScript which you may need to convert for use in your scripts.这将在 JavaScript 中生成一个字符串值,您可能需要将其转换为在您的脚本中使用。 If the PHP value is numeric, you don't need to include the quotes.如果 PHP 值是数字,则不需要包含引号。

Here we demonstrate with boolean, numeric, and string values assigned to PHP variables:这里我们演示了分配给 PHP 变量的布尔值、数字值和字符串值:

<?php
$bool = false;
$num = 3 + 4;
$str = "A string here";
?>

You could output them into JavaScript with the following:您可以使用以下命令将它们输出到 JavaScript 中:

<script type="text/javascript">
// boolean outputs "" if false, "1" if true
var bool = "<?php echo $bool ?>"; 

// numeric value, both with and without quotes
var num = <?php echo $num ?>; // 7
var str_num = "<?php echo $num ?>"; // "7" (a string)

var str = "<?php echo $str ?>"; // "A string here"
</script>

There are some conditions under which strings that are valid in PHP may generate errors in JavaScript when output using this approach.在某些情况下,在 PHP 中有效的字符串在使用此方法输出时可能会在 JavaScript 中产生错误。 The PHP json_encode function can be used to resolve these problems as well as to preserve data type of booleans and numbers. PHP json_encode 函数可用于解决这些问题以及保留布尔值和数字的数据类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM