JavaScript innerHTML 包括 php
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29443759/
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
JavaScript innerHTML include php
提问by User
Why doesn't my code work?
为什么我的代码不起作用?
function _(x){
return document.getElementById(x);
}
function friends(){
_("right").innerHTML = '<?php include_once "friendlist.php";?>';
}
Result:
结果:
<!--?php include_once "friendlist.php";?-->
How can I fix it?
我该如何解决?
采纳答案by Nishit Maheta
use below code it will work
使用下面的代码它会工作
<div id="right">
<script>
document.write('<?php echo include_once "include.php";?>');
</script>
</div>
or i test below code also it will work
或者我测试下面的代码它也可以工作
<body>
<div id ="right" >
</div>
</body>
<script>
function friends(){
var x=document.getElementById("right");
x.innerHTML = '<?php include_once "include.php";?>';
}
friends();
</script>
回答by Ayyanar G
you have to do like this
你必须这样做
$(document).ready(function(){
$("#your_id").click(function(){
$("#your_div").load('friendlist.php');
});
});
回答by Vivek Singh
<script>
$(document).ready(function(){
$("#id").click(function(){
$("#div").load('friendlist.php');
});
});
</script>
try like this...
像这样尝试...
回答by user3252197
it's Impossible because php works on server not in the browser. use:
这是不可能的,因为 php 在服务器上运行而不是在浏览器中运行。用:
$("div").load("php_file_name")
$("div").load("php_file_name")
回答by CoR
If your code could work, it would be serious breach of security. Good thing is that you CAN NOT inject and execute php code by your browser:) You can put php tags in html file, you can display it, but it will not even pass by the server, let alone execute it.
如果你的代码可以工作,那将是严重的安全漏洞。好消息是你不能通过你的浏览器注入和执行 php 代码:) 你可以把 php 标签放在 html 文件中,你可以显示它,但它甚至不会通过服务器,更不用说执行它了。
On the other hand you CAN create friendlist.php on the serverand make sure:
另一方面,您可以在 上创建friendlist.phpserver并确保:
- page echoes html data that you want
- page is accessible by url like http://www.your_site.com/friendlist.php
- 页面回显你想要的 html 数据
- 页面可通过 url 访问,如http://www.your_site.com/friendlist.php
.
.
Than you do your html/js in browser/client
比你做你的 html/js browser/client
_("right").innerHTML = ajax('http://yout_site.com/friendlist.php');
Btw, if you use JavaScript to request something from server and then display it in browser it's called ajax ;)
顺便说一句,如果你使用 JavaScript 从服务器请求一些东西,然后在浏览器中显示它,它被称为 ajax ;)
Just for fun, you could also use http://www.upiur_site.com/friendlist.php'> to avoid ajax. But that has many disadvantages. For ajax function use library, write your own...
只是为了好玩,您也可以使用 http://www.upiur_site.com/friendlist.php'> 来避免 ajax。但这有很多缺点。对于ajax函数使用库,自己写...
EDIT:
编辑:
I missed third option if server is set to parse .js files. In that case will work.
如果服务器设置为解析 .js 文件,我错过了第三个选项。在这种情况下会起作用。
回答by Stuperfied
I know this is asked and answered and also that it is in relation to static content, however its worth pointing out that these answers (non ajax) require the whole page to be refreshed in order to update the php content. This is because the php include is compiled by the server and then sent to the browser where the javascript now has only the results of the compilation.
我知道这是问和回答,而且它与静态内容有关,但是值得指出的是,这些答案(非 ajax)需要刷新整个页面才能更新 php 内容。这是因为 php 包含由服务器编译,然后发送到浏览器,javascript 现在只有编译结果。
Users reading this may wish to create dynamic content, in which case a better solution may be to have the javascript fetch the page. This can be achieved as follows.
阅读本文的用户可能希望创建动态内容,在这种情况下,更好的解决方案可能是让 javascript 获取页面。这可以如下实现。
<script type="text/javascript">
function updatePrices() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("demo").innerHTML = this.responseText;
}
};
xhttp.open("GET", "http://example.com/current_prices.php", true);
xhttp.send();
}
</script>
<div id="demo">
<h2>Change This</h2>
</div>
<button type="button" onclick="updatePrices();">Click Here!</button>
ref:
https://www.experts-exchange.com/questions/29091951/Include-php-file-with-JS.htmlhttps://www.w3schools.com/xml/xml_http.asp
参考:
https: //www.experts-exchange.com/questions/29091951/Include-php-file-with-JS.html https://www.w3schools.com/xml/xml_http.asp
A javascript setTimeout() function may also be used if the content needs to be refreshed on a timer.
如果需要在计时器上刷新内容,也可以使用 javascript setTimeout() 函数。
Please feel free to improve my answer or to append it to other similar questions.
请随时改进我的答案或将其附加到其他类似问题。

