2012-02-17 66 views
1

我在服务器端收到一个未定义的索引错误消息,当我试图从客户端(JavaScript/AJAX)的下拉框中选择一个值到服务器端(PHP) 。我有一种感觉,在客户端的GET没有正确地发送URL或如果它是发送值为空。AJAX xmlHttp请求GET undefined索引XML读取文件

有没有人有任何想法如何解决这个问题。 客户端上的代码是:

<title>test</title> 
<script type="text/javascript" src="jquery.js"></script> 

<script type="text/javascript"> 
$(document).ready(function() 
{ 
    $.getJSON("process.php", function(fileName) 
{   
    for(var i in fileName) 
    { 
     fileName[i] = fileName[i].split("../Test/").join("") 
     fileName[i] = fileName[i].split(".xml").join("") 
     document.dropDown.file[i]= new Option(fileName[i],"../Test/"+fileName[i]+".xml", false) 
    }  
}); 

}); 

var xmlhttp; 
function getFile(str){ 
alert("xmlprocess.php?filename="+str); 
if (str=="") 
{ 
    document.getElementById("txtHint").innerHTML=""; 
    return; 
} 
if (window.XMLHttpRequest) 
{// code for IE7+, Firefox, Chrome, Opera, Safari 
xmlhttp=new XMLHttpRequest(); 
} 
else 
{// code for IE6, IE5 
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
} 
xmlhttp.onreadystatechange= response(file); 
{ 
if (xmlhttp.readyState==4 && xmlhttp.status==200) 
{ 
document.getElementById("txtHint").innerHTML= xmlhttp.responseText; 
} 
} 

xmlhttp.open("GET","xmlprocess.php?filename="+str,true); 
xmlhttp.send(); 
} 

</script> 
</head> 
<body> 

<form name = "dropDown"> 
<Select name = "file" onclick = "getFile(str1)" onchange = "str1 = this.options[this.selectedIndex].value"></Select> 
</form> 

<div id="txtHint"></div> 
</html> 

,并在服务器端的代码是:

<?php 
ini_set('display_errors',1); 
error_reporting(E_ALL); 

$br = "<br>"; 
$filename = $_GET["filename"]; 
echo $filename; 
?> 

回答

0

玉家伙香港专业教育学院找到了答案

我不得不改变的onreadystatechange相等到客户端的功能如下所示

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