2012-01-04 72 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