[英]php / Javascript / Ajax loading content does not work
I'm trying to load content from a php file which name is "include.php" to a in another php file which name is "index.php". 我正在尝试从名称为“ include.php”的php文件中的内容加载到名称为“ index.php”的另一个php文件中。 But the loading does not work. 但是加载不起作用。 The code is as below: 代码如下:
The file: index.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>
The File: include.php 文件:include.php
<?php
echo 'Hello!';
?>
Thanks. 谢谢。
If what you are doing is the way you describe then this is simpler: 如果您正在做的是您描述的方式,那么这会更简单:
<header>
<?php include("include.php") ?>
</head>
<body>
<input type="submit" value="Submit" onclick="load();">
<div id="adiv"></div>
</body>
If you want to get result from include.php
into JavaScript then you'll probably be better using ajax. 如果您希望将include.php
结果获取到JavaScript中,那么使用Ajax可能会更好。
By the way, if you are planning a "universal header" for all your PHP files, you don't need to echo it just write it as normal HTML with any necessary PHP tags 顺便说一句,如果您正在为所有PHP文件计划一个“通用标头”,则无需回显它,只需将其写为带有任何必要PHP标记的普通HTML即可。
Should it not be document.getElementById("adiv").innerHTML = xmlhttp.responseText;
如果不是document.getElementById("adiv").innerHTML = xmlhttp.responseText;
? ? Notice the quotes. 注意引号。
....xmlhttp.onreadystatechage = function (){ if(xmlhttp.readyState == 4 && xmlhttp.st...
检查onReadyStateChange
的拼写。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.