我正在尝试将名称为“include.php”的 php 文件中的内容加载到另一个名称为“index.php”的 php 文件中。但加载不起作用。代码如下:
文件:index.php
<header>
<script type="text/javascript">
function load(){
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
}else{
xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
}
xmlhttp.onreadystatechage = function (){
if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
document.getElementById(adiv).innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open('GET', 'include.php', true);
xmlhttp.send();
}
</script>
</head>
<body>
<input type="submit" value="Submit" onclick="load();">
<div id="adiv"></div>
</body>
文件:include.php
<?php
echo 'Hello!';
?>
谢谢。
最佳答案
如果您所做的就是您所描述的方式,那么这会更简单:
<header>
<?php include("include.php") ?>
</head>
<body>
<input type="submit" value="Submit" onclick="load();">
<div id="adiv"></div>
</body>
如果您想将 include.php
的结果转换为 JavaScript,那么您可能会更好地使用 ajax。
顺便说一句,如果您计划为所有 PHP 文件使用“通用 header ”,则无需回显它,只需将其写为带有任何必要 PHP 标记的普通 HTML
关于javascript - php/Javascript/Ajax 加载内容不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30962070/