如何从 JavaScript 读取 PHP 变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10582024/
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
How to read PHP variable from JavaScript?
提问by QLands
I know that there are many questions about this but I cannot make it work.
我知道对此有很多问题,但我无法让它发挥作用。
My HTML (test.htm) has only this code
我的 HTML (test.htm) 只有这个代码
<?php
$var = 'foo';
?>
<script type="text/javascript" language="javascript">
var val = "<?=$var?>";
alert(val);
</script>
But when I open the file with the browser the value of val is "<?=$var?>"
and not 'foo'
但是当我用浏览器打开文件时,val 的值是"<?=$var?>"
而不是 'foo'
How can I make it work?
我怎样才能让它工作?
回答by Wesley Murch
Unless you have some configuration to allow it, .htm
files won't execute PHP code, you'll have to use a .php
file.
除非您有一些配置允许,否则.htm
文件不会执行 PHP 代码,您必须使用.php
文件。
If you look at your HTML page source in the browser, you'll probably see all the PHP code.
如果您在浏览器中查看 HTML 页面源代码,您可能会看到所有 PHP 代码。
The only other explanation is that short tags <? ?>
aren't enabled, you'll have to use<?php echo $var; ?>
唯一的其他解释是<? ?>
未启用短标签,您必须使用<?php echo $var; ?>
回答by T.J. Crowder
But when I open the file with the browser the value of val is "" and not 'foo'
但是当我用浏览器打开文件时,val 的值是 "" 而不是 'foo'
Sounds like you have shorttags disabled(and are using PHP < 5.4.0). Try
听起来您禁用了短标签(并且正在使用 PHP < 5.4.0)。尝试
var val = "<?php echo $var ?>";
Edit: And note CM Kanode's comment on the question: If it's a .htm
file, odds are that your server isn't running it through PHP at all (that would take special configuration and probably not be a good idea). (You are opening this via an http://
URL, right? Not opening the file locally? Because unless the PHP server gets involved, the PHP tags can't be processed.)
编辑:并注意 CM Kanode 对这个问题的评论:如果它是一个.htm
文件,很可能你的服务器根本没有通过 PHP 运行它(这需要特殊配置,可能不是一个好主意)。(你是通过http://
URL打开这个,对吧?不是在本地打开文件?因为除非 PHP 服务器参与,否则无法处理 PHP 标签。)
And better yet, let json_encode
make sure the value is property quoted and such for you:
更好的是,让json_encode
我们确保该值是引用的属性,对您来说是这样的:
var val = <?php echo json_encode($var) ?>;
回答by errkk
Maybe you don't have shorttags enabled, try
也许你没有启用短标签,试试
You might also want to look out for escaping of string and stuff, so if you have something more complex than a string, you could use JSON
您可能还想注意字符串和其他内容的转义,因此如果您有比字符串更复杂的内容,则可以使用 JSON
<?php $var = array( 'stuff' => 'things' );?>
<?php echo json_encode($var);?>
回答by davidethell
Your post says that your file extension is .htm. Do you have your web server set to parse .htm files as PHP? If your server is only parsing .php files rename your file and try again as that would explain why the isn't being processed. If it is set to parse .htm files then T.J. Crowder's answer is the most likely issue.
您的帖子说您的文件扩展名是 .htm。您是否已将 Web 服务器设置为将 .htm 文件解析为 PHP?如果您的服务器仅解析 .php 文件,请重命名您的文件并重试,因为这将解释为什么没有被处理。如果它设置为解析 .htm 文件,那么 TJ Crowder 的回答是最有可能的问题。
回答by Sudhir Bastakoti
try:
尝试:
var val = "<?php echo $var; ?>";