php 如何将php变量显示为html
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8765855/
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 display a php variable into html
提问by BruceyBandit
I just want to know how can I display a javascript variable into html?
我只想知道如何将 javascript 变量显示为 html?
Below is my php code:
下面是我的php代码:
var duration = <? echo $postduration; ?>;
(The variable above uses php to retrieve a php variable)
(上面的变量使用php来检索一个php变量)
Thank you
谢谢
回答by blake305
Why don't use just display the php variable directly into the html?
为什么不直接在html中显示php变量呢?
Lets just say $postduration
echos "some." Here is the javascript code to echo "some":
让我们说$postduration
回声“一些”。这是回显“some”的javascript代码:
Here is <script>document.write(duration)</script> text.
Here will be the output:
这里将是输出:
Here is some text.
回答by paislee
Make your code more concise with jQuery:
使用jQuery使您的代码更简洁:
(1) Add this in your <head>
:
(1)在您的<head>
:
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
(2) Create an element where you want the variable to be displayed, and give it an ID, e.g.:
(2) 创建一个要显示变量的元素,并给它一个ID,例如:
<span id="printHere"></span>
(3) Add this in your JavaScript:
(3) 在你的 JavaScript 中添加:
var duration="<?php echo $postduration ?>";
$('#printHere').html(duration);
For your PHP, try the following two options:
对于您的 PHP,请尝试以下两个选项:
<?=$postduration?>
or...
或者...
<?php echo $postduration; ?>
回答by Atonewell
document.getElementById('yourElementId').innerHtml = duration;
回答by mrk
Here's a way to add a DIV to a page with the content of the DIV being the value of your PHP var:
这是一种将 DIV 添加到页面的方法,该 DIV 的内容是您的 PHP 变量的值:
<script>
var duration = <? echo $postduration; ?>;
var div = document.createElement("DIV");
div.appendChild(document.createTextNode(duration));
document.body.appendChild(div);
</script>
回答by Alxandr
No, wait, that's wrong. First of all, the javascript above does not "retrieve" anything from anywhere (neither php, nor anyplace else). Php generates the javascript, meaning that if $postduration is 5, you will get var duration = 5;
, and if postduration = "test", you will get var duration = test;
(which is invalid javascript and willcrash/cause unwanted things to happen in your application.
不,等等,这是错误的。首先,上面的 javascript 不会从任何地方“检索”任何东西(既不是 php,也不是其他任何地方)。PHP生成JavaScript,也就是说,如果$ postduration是5,你会得到var duration = 5;
,如果postduration =“测试”,你会得到var duration = test;
(这是无效的JavaScript和会崩溃/引起不必要的东西,在你的应用程序发生。
Last, to get a variable from javascript into html, you either replace the content of some other object (with innerHTML
or something like that), or you create a new element and insert that into the page.
最后,要将变量从 javascript 获取到 html,您要么替换其他对象的内容(使用innerHTML
或类似的内容),要么创建一个新元素并将其插入到页面中。
Example here: http://jsfiddle.net/SLbKX/
这里的例子:http: //jsfiddle.net/SLbKX/