我仔细检查了我的代码,找不到我做错了哪一部分。每次我单击该按钮时,它都不会从我生成的文件中检索文件.php
指数。.PHP
<html>
<head>
<title>Title</title>
<script type="text/javascript">
function myLoad(){
if(window.XMLHttpRequest){
xmlhttp = new XMLHttpRequest();
}else{
xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
}
xmlhttp.onreadystatechange = function(){
if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
document.getElementById('par').innerHTML == xmlhttp.responseText;
}
}
xmlhttp.open('GET', 'generate.php', true);
xmlhttp.send();
}
</script>
</head>
<body>
<div id="par"></div>
<input type="button" value="Click" onclick="myLoad();">
</body>
</html>
生成。.PHP
<?php
echo 'Hello';
?>
document.getElementById('par').innerHTML == xmlhttp.responseText;
^ // here is problem it should be =
document.getElementById('par').innerHTML = xmlhttp.responseText;
这里的
错别字:
document.getElementById('par').innerHTML == xmlhttp.responseText;
------------------------------------------^ // Make it just =
将此代码复制到 index.php 文件中。 我相信它会解决您的问题,当您单击按钮时,它会从生成.php文件中调用"您好"。
<html>
<head>
<title>Title</title>
<script type="text/javascript">
function myLoad()
{
var xmlhttp;
if (window.XMLHttpRequest){
xmlhttp=new XMLHttpRequest();
}
else{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200){
document.getElementById("par").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","generate.php",true);
xmlhttp.send();
}
</script>
</head>
<body>
<input type="button" value="Click" onclick="myLoad();">
<div id="par"></div>
</body>
</html>