2012-01-04 74 views
0

情景是,下面是我想要顯示div id =「myDiv」文本文件內容的html文件。如何使用ajax發送JavaScript變量到php?

該文件將被php讀取。下面給出了php的內容。 我無法從文本文件中獲取內容。請告訴我應該在ajax部分添加什麼來糾正程序。

<html> 
<head> 
<script type="text/javascript"> 

function ajaxRequest(tb) { 
var xmlhttp; 
if (window.XMLHttpRequest) 
{ 
    xmlhttp=new XMLHttpRequest(); 
} 
    xmlhttp.onreadystatechange=statechange() 
    { 
     if (xmlhttp.readyState==4 && XMLHttpRequestObject.status == 200) 
     { 
     document.getElementById("myDiv").innerHTML=xmlhttp.responseText; 
     } 
    } 
xmlhttp.open("GET","http://localhost/TBR/getdata.php?tb="+tb,true); 
xmlhttp.send(); 
}</script> 

</head> 

<body> 
<div > 
     <div id="myDiv">Text from file should come here.</div> 
     <button type="button" onclick="ajaxRequest(myfile.txt)">Change Content</button> 
</div> 
</body> 
</html> 




Below is the PHP file 

<?php 
    $tbName = $_GET['tb']; 
    $file = "/home/$tbName"; 
    $f = fopen("$file", "r"); 
    echo "<ul>"; 
    while(!feof($f)) { 
    $a= fgets($f); 


    echo "<li>$a</li><hr/>"; 

    } 
    echo "</ul>"; 

?> 
+0

哪位不工作? PHP被調用了嗎?瀏覽器錯誤? javascript驗證? – ManseUK 2012-01-04 14:45:43

回答

0

<button type="button" onclick="ajaxRequest('myfile.txt')">Change Content</button>

記得引用在字符串中的onclick。

這裏有一個固定的版本:

<html> 
<head> 
<script type="text/javascript"> 
function ajaxRequest(tb) { 
    if (window.XMLHttpRequest){ 
    var xmlhttp= new XMLHttpRequest(); 
    xmlhttp.onreadystatechange=function(){ 
     if (xmlhttp.readyState==4 && xmlhttp.status == 200){ 
     document.getElementById("myDiv").innerHTML=xmlhttp.responseText; } 
    } 
    xmlhttp.open("GET","http://localhost/TBR/getdata.php?tb="+ encodeURIComponent(tb),true); 
    xmlhttp.send(); 
    } 
} 
</script> 
</head> 
<body> 
     <div id="myDiv">Text from file should come here.</div> 
     <button type="button" onclick="ajaxRequest('myfile.txt')">Change Content</button> 
</body> 
</html> 

什麼是錯的:

  • 的XMLHttpRequest的情況下進行了測試,但功能沒有包裹在一個如果,使得無用。
  • 變量名都有些不匹配的 - 仔細檢查
  • 是encodeURI組件,如下面提到
  • 的正確語法的回調函數window.onload = function(){ alert('func!'); }window.onload = alert(){ alert('load!'); }

這應該是它,除非有PHP腳本的問題,請嘗試通過直接訪問URL來測試。

+0

我添加了報價,但仍然無法正常工作。請幫忙。 – Logesh 2012-01-04 13:17:51

0

修復行情

onclick="ajaxRequest('myfile.txt')" 

確保您在瀏覽器中使用encodeURIComponent方法()對結核病

xmlhttp.open("GET","http://localhost/TBR/getdata.php?tb="+encodeURIComponent(tb),true); 

測試你的PHP頁面:不http://localhost/TBR/getdata.php?tb=myfile.txt提供你想要的數據?

如果是這樣測試函數被調用。 (提醒或調試代碼,並在函數中放置斷點,或者如果瀏覽器支持使用console.debug)

如果該函數被調用,那麼您的事件處理程序工作正常,如果不嘗試重寫它或附加不同的像onclick =「ajaxRequest('myfile.txt');」雖然我懷疑缺少分號不是問題。

如果這個被調用,你可以嘗試查看ajax調用是否在我檢查網絡流量時執行。任何體面的瀏覽器都會讓你這樣做(點擊f12並查找網絡標籤)。如果正在發出並響應ajax請求,您應該能夠看到請求和響應。

假設一切正常,請確保您的事件ajax事件處理程序正在調用。我懷疑這裏有個問題,因爲你不是事件處理程序設置爲功能...

xmlhttp.onreadystatechange = function statechange() 
{ 
    if (xmlhttp.readyState==4 && XMLHttpRequestObject.status == 200) 
    { 
     document.getElementById("myDiv").innerHTML=xmlhttp.responseText; 
    } 
} 

和失敗的所有您的數據插入代碼是行不通的。

+0

我添加了報價,但它不起作用。我的php代碼也在那裏。請看看和幫助。 – Logesh 2012-01-04 13:17:22