php / Javascript / Ajax加载内容不起作用

时间:2015-06-21 06:57:54

标签: javascript php ajax

我试图从php文件中加载内容,其名称为" include.php"到另一个php文件,其名称是" 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>

文件:include.php

<?php
    echo 'Hello!';
?>

感谢。

3 个答案:

答案 0 :(得分:1)

如果你正在做的是你描述的方式,那么这更简单:

include.php

如果您希望将{{1}}的结果导入JavaScript,那么您可能会更好地使用ajax。

顺便说一句,如果你正在计划一个&#34;通用标题&#34;对于所有PHP文件,您不需要回复它,只需将其写为普通HTML,并附带任何必要的PHP标记

答案 1 :(得分:0)

不应该document.getElementById("adiv").innerHTML = xmlhttp.responseText;吗?注意引号。

答案 2 :(得分:0)

....xmlhttp.onreadystatechage = function (){ if(xmlhttp.readyState == 4 && xmlhttp.st...

检查拼写onReadyStateChange