2012-02-17 67 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; 
    } 
};