2015-06-21 37 views
0

我試圖從名爲「include.php」的php文件中加載內容到另一個名爲「index.php」的php文件中。但加載不起作用。代碼如下:php/Javascript/Ajax加載內容不起作用

文件: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!'; 
?> 

感謝。

+0

轉到檢查元素 - >網絡選項卡 - >你能在那裏看到什麼? –

+0

我有三個文件的列表:「index.php」,「style.css」和「include.inc.php」。所有這三個文件的方法是「GET」。 「index.php」和「include.inc.php」的狀態是「OK」。 「style.css」的狀態是「未修改」。 – Sara

回答

0

是不是document.getElementById("adiv").innerHTML = xmlhttp.responseText;?注意引號。

+0

謝謝,我糾正了這個錯誤,但仍然不起作用。 – Sara

+0

現在有效。我改變了'xmlhttp.onReadyStateChange = function(){'回到'xmlhttp.onreadystatechange'並更正了'getElementById(「adiv」)'。謝謝你的幫助。 @Nitesh Kumar Anand,@Kirs Sudh,@Michael Christensen,@ Kunal Pradhan – Sara

1

如果你在做什麼是你所描述的方式,那麼這是簡單的:

<header> 
<?php include("include.php") ?> 
</head> 
<body> 
    <input type="submit" value="Submit" onclick="load();"> 
    <div id="adiv"></div> 
</body> 

如果你想從include.php導致成JavaScript,那麼你可能會得到更好的使用AJAX。

順便說一句,如果你正在計劃一個「萬能頭部」爲所有PHP文件,你不需要爲了呼應它只是它與任何必要的PHP代碼寫正常的HTML

+0

嘿,如果你喜歡答案,你可以給一個複選標記來表示這個問題已被回答? –

+0

謝謝邁克爾。我想要做的是從另一個文件加載一些內容到div而不刷新頁面。在這種情況下,當我單擊「提交」按鈕時,應該在「提交」按鈕下加載另一文件上的內容,但不刷新頁面。 – Sara

0

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

檢查拼寫onReadyStateChange

+0

我改正了這個錯誤,但它仍然不起作用。 – Sara

相關問題