在JavaScript中访问PHP变量 [英] Access PHP variable in JavaScript

查看:114
本文介绍了在JavaScript中访问PHP变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

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

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

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

我在PHP中有一个变量 $ a ,并希望在JavaScript变量中获取它的值。

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

推荐答案

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

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

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

您也可以使用AJAX加载

You can also load it with AJAX

rhino是对的,为了简洁起见,片段缺少类型。

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

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

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

这篇关于在JavaScript中访问PHP变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆