在JavaScript中访问PHP变量[重复]

时间:2022-08-27 17:20:03

Possible Duplicate:
How to access PHP variables in JavaScript or jQuery rather than <?php echo $variable ?>

可能重复:如何在JavaScript或jQuery中访问PHP变量而不是<?php echo $ variable?>

Is there any way to get access to a PHP variable in JavaScript?

有没有办法在JavaScript中访问PHP变量?

I have a variable, $a, in PHP and want to get its value in a JavaScript variable.

我在PHP中有一个变量$ a,想要在JavaScript变量中获取它的值。

3 个解决方案

#1


74  

You can't, you'll have to do something like

你不能,你必须做类似的事情

<script type="text/javascript">
   var php_var = "<?php echo $php_var; ?>";
</script>

You can also load it with AJAX

您也可以使用AJAX加载它

rhino is right, the snippet lacks of a type for the sake of brevity.

犀牛是对的,为简洁起见,片段缺少一种类型。

Also, note that if $php_var has quotes, it will break your script. You shall use addslashes, htmlentities or a custom function.

另请注意,如果$ php_var有引号,它将破坏您的脚本。您应使用addslashes,htmlentities或自定义函数。

#2


41  

metrobalderas is partially right. Partially, because the PHP variable's value may contain some special characters, which are metacharacters in JavaScript. To avoid such problem, use the code below:

metrobalderas部分正确。部分地,因为PHP变量的值可能包含一些特殊字符,这些字符是JavaScript中的元字符。要避免此类问题,请使用以下代码:

<script type="text/javascript">
var something=<?php echo json_encode($a); ?>;
</script>

#3


0  

I'm not sure how necessary this is, and it adds a call to getElementById, but if you're really keen on getting inline JavaScript out of your code, you can pass it as an HTML attribute, namely:

我不确定这是多么必要,并且它添加了对getElementById的调用,但如果您真的热衷于从代码中获取内联JavaScript,则可以将其作为HTML属性传递,即:

<span class="metadata" id="metadata-size-of-widget" title="<?php echo json_encode($size_of_widget) ?>"></span>

And then in your JavaScript:

然后在你的JavaScript中:

var size_of_widget = document.getElementById("metadata-size-of-widget").title;

#1


74  

You can't, you'll have to do something like

你不能,你必须做类似的事情

<script type="text/javascript">
   var php_var = "<?php echo $php_var; ?>";
</script>

You can also load it with AJAX

您也可以使用AJAX加载它

rhino is right, the snippet lacks of a type for the sake of brevity.

犀牛是对的,为简洁起见,片段缺少一种类型。

Also, note that if $php_var has quotes, it will break your script. You shall use addslashes, htmlentities or a custom function.

另请注意,如果$ php_var有引号,它将破坏您的脚本。您应使用addslashes,htmlentities或自定义函数。

#2


41  

metrobalderas is partially right. Partially, because the PHP variable's value may contain some special characters, which are metacharacters in JavaScript. To avoid such problem, use the code below:

metrobalderas部分正确。部分地,因为PHP变量的值可能包含一些特殊字符,这些字符是JavaScript中的元字符。要避免此类问题,请使用以下代码:

<script type="text/javascript">
var something=<?php echo json_encode($a); ?>;
</script>

#3


0  

I'm not sure how necessary this is, and it adds a call to getElementById, but if you're really keen on getting inline JavaScript out of your code, you can pass it as an HTML attribute, namely:

我不确定这是多么必要,并且它添加了对getElementById的调用,但如果您真的热衷于从代码中获取内联JavaScript,则可以将其作为HTML属性传递,即:

<span class="metadata" id="metadata-size-of-widget" title="<?php echo json_encode($size_of_widget) ?>"></span>

And then in your JavaScript:

然后在你的JavaScript中:

var size_of_widget = document.getElementById("metadata-size-of-widget").title;