我已经包含了JavaScript,并且我想在JavaScript中运行一个包含脚本。我尝试了以下语句:
outputEl.innerHTML = '<?php include "./change/ud1.php";?>';当我运行脚本时,我没有得到任何响应。
有没有人知道为什么我没有得到任何回应,以及我如何修复它?
我尝试用html iframe替换PHP include。这起作用了,但这对我来说不是最好的解决方案。
发布于 2017-07-10 20:26:39
在这种情况下不需要使用javascript
你可以在你想要的地方直接使用响应
<div id="outputEl"><?php include "./change/ud1.php";?></div>发布于 2017-07-06 18:35:28
你不能在.js文件中运行php。据我所知,您需要从js文件中获取php文件的内容。
这是一个基本的实现。
var xhr = new XMLHttpRequest();
xhr.onload = function () {
outputEl.innerHTML = this.response;
};
xhr.open('GET', './change/ud1.php', true); // you may need to correct the url to be relative to the js file
xhr.send();使用jquery可能更简单、更健壮,在这种情况下(一旦包含了jquery库),您可以使用如下所示的内容
$(outputEl).load("change/ud1.php");发布于 2017-07-06 20:41:10
所以php只能在你的服务器上执行,而JavaScript只能在用户的浏览器上执行。
php代码不可能在JavaScript (浏览器)中运行。所以你需要像这样使用ajax。
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
// server sent data successfully in this.responseText variable
document.getElementById("output1").innerHTML = this.responseText;
}
};
//Request data from a server
xhttp.open("GET", "path/to/php/include-script.php", true);
xhttp.send();了解有关ajax here的更多信息。
https://stackoverflow.com/questions/44868300
复制相似问题