JavaScript innerHTML 包含 php
JavaScript innerHTML include php
为什么我的代码不起作用?
function _(x){
return document.getElementById(x);
}
function friends(){
_("right").innerHTML = '<?php include_once "friendlist.php";?>';
}
结果:
<!--?php include_once "friendlist.php";?-->
我该如何解决?
你必须这样做
$(document).ready(function(){
$("#your_id").click(function(){
$("#your_div").load('friendlist.php');
});
});
使用下面的代码就可以了
<div id="right">
<script>
document.write('<?php echo include_once "include.php";?>');
</script>
</div>
或者我测试下面的代码也可以工作
<body>
<div id ="right" >
</div>
</body>
<script>
function friends(){
var x=document.getElementById("right");
x.innerHTML = '<?php include_once "include.php";?>';
}
friends();
</script>
<script>
$(document).ready(function(){
$("#id").click(function(){
$("#div").load('friendlist.php');
});
});
</script>
像这样尝试...
这是不可能的,因为 php 在服务器而不是在浏览器中工作。
使用:
$("div").load("php_file_name")
如果您的代码可以工作,那将是严重的安全漏洞。好消息是你不能通过浏览器注入和执行 php 代码:) 你可以将 php 标签放在 html 文件中,你可以显示它,但它甚至不会通过服务器,更不用说执行它了。
另一方面,您可以在 server
上创建 friendlist.php 并确保:
- 页面回显 html 您想要的数据
- 页面可由 url 访问,喜欢 http://www.your_site.com/friendlist.php
。
比你在 browser/client
中的 html/js
_("right").innerHTML = ajax('http://yout_site.com/friendlist.php');
顺便说一句,如果你使用 JavaScript 从服务器请求一些东西然后在浏览器中显示它叫做 ajax ;)
为了好玩,您也可以使用 http://www.upiur_site.com/friendlist.php'> 来避免 ajax。但这有很多缺点。
对于ajax函数使用库,自己写...
编辑:
如果服务器设置为解析 .js 文件,我错过了第三个选项。在那种情况下会起作用。
我知道有人询问并回答了这个问题,而且它与静态内容有关,但值得指出的是,这些答案(非 ajax)需要刷新整个页面才能更新php 内容。这是因为 php 包含由服务器编译,然后发送到浏览器,其中 javascript 现在只有编译结果。
阅读本文的用户可能希望创建动态内容,在这种情况下,更好的解决方案可能是让 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>
参考:
https://www.experts-exchange.com/questions/29091951/Include-php-file-with-JS.html
https://www.w3schools.com/xml/xml_http.asp
如果内容需要在计时器上刷新,也可以使用 javascript setTimeout() 函数。
请随时改进我的回答或将其附加到其他类似问题。
为什么我的代码不起作用?
function _(x){
return document.getElementById(x);
}
function friends(){
_("right").innerHTML = '<?php include_once "friendlist.php";?>';
}
结果:
<!--?php include_once "friendlist.php";?-->
我该如何解决?
你必须这样做
$(document).ready(function(){
$("#your_id").click(function(){
$("#your_div").load('friendlist.php');
});
});
使用下面的代码就可以了
<div id="right">
<script>
document.write('<?php echo include_once "include.php";?>');
</script>
</div>
或者我测试下面的代码也可以工作
<body>
<div id ="right" >
</div>
</body>
<script>
function friends(){
var x=document.getElementById("right");
x.innerHTML = '<?php include_once "include.php";?>';
}
friends();
</script>
<script>
$(document).ready(function(){
$("#id").click(function(){
$("#div").load('friendlist.php');
});
});
</script>
像这样尝试...
这是不可能的,因为 php 在服务器而不是在浏览器中工作。 使用:
$("div").load("php_file_name")
如果您的代码可以工作,那将是严重的安全漏洞。好消息是你不能通过浏览器注入和执行 php 代码:) 你可以将 php 标签放在 html 文件中,你可以显示它,但它甚至不会通过服务器,更不用说执行它了。
另一方面,您可以在 server
上创建 friendlist.php 并确保:
- 页面回显 html 您想要的数据
- 页面可由 url 访问,喜欢 http://www.your_site.com/friendlist.php
。
比你在 browser/client
_("right").innerHTML = ajax('http://yout_site.com/friendlist.php');
顺便说一句,如果你使用 JavaScript 从服务器请求一些东西然后在浏览器中显示它叫做 ajax ;)
为了好玩,您也可以使用 http://www.upiur_site.com/friendlist.php'> 来避免 ajax。但这有很多缺点。 对于ajax函数使用库,自己写...
编辑:
如果服务器设置为解析 .js 文件,我错过了第三个选项。在那种情况下会起作用。
我知道有人询问并回答了这个问题,而且它与静态内容有关,但值得指出的是,这些答案(非 ajax)需要刷新整个页面才能更新php 内容。这是因为 php 包含由服务器编译,然后发送到浏览器,其中 javascript 现在只有编译结果。
阅读本文的用户可能希望创建动态内容,在这种情况下,更好的解决方案可能是让 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>
参考:
https://www.experts-exchange.com/questions/29091951/Include-php-file-with-JS.html
https://www.w3schools.com/xml/xml_http.asp
如果内容需要在计时器上刷新,也可以使用 javascript setTimeout() 函数。
请随时改进我的回答或将其附加到其他类似问题。