2012-12-19 183 views
0

我创建了一个使用PHP的网页,并试图根据用户选择加载XML文件。我看了各种网站和论坛,看看我是否做错了什么,但即使我尝试复制代码似乎并不工作。我得出的结论是,我需要一双新的眼睛来注意我可能没有的东西。当我从下拉列表中选择一个项目时,我希望它加载选定的XML文件,并显示其中的信息,但使用代码我迄今为止没有任何事情发生。我从下拉列表中选择一个选项,但没有任何反应。我认为这是加载XML文件的问题,因为当我更改loadXML()函数中的代码以输出所选的选项时,它就起作用了。我只是不明白为什么它不起作用。任何帮助将不胜感激。PHP XML DOM - 加载XML文件

<html> 
<head> 
<h1><u>State Information</u></h1> 
</head> 
<body> 
<p><b>Please select an area of the US in the dropdown list below.</b></p> 
<p><select name="area" onchange="loadXML(this.value)"> 

<?php 
//set directory and open it 
$xmldir = 'XML'; 
$dir = opendir($xmldir); 

//create array and read through files in directory 
$xmlfiles = array(); 
while ($file = readdir($dir)) 
{ 
//if the first char is not '.' then add to array 
if (substr($file,-1,1) !== ".") 
{ 
    $xmlfiles[] = $file; 
} else 
{ 
    //do nothing 
} 
} 

echo '<option value="select">Select</option>'; 

foreach($xmlfiles as $area){ 
     echo '<option value="'.$area.'">'.$area.'</option>'; 
} 
echo '</select>'; 

//close directory 
closedir($dir); 
?> 

</p> 
</body> 
</html> 

<script> 
function loadXML($area) { 
if (window.XMLHttpRequest) 
{ 
    xhttp=new XMLHttpRequest(); 
} 
else 
{ 
    xhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
} 
xhttp.open("GET",$area,false); 
xhttp.send(); 
xmlDoc = xhttp.responseXML; 
x=xmlDoc.getElementsByTagName("name"); 

for (i=0;i<x.length;i++) 
     { 
    document.write(x[i].childNodes[0].nodeValue); 
    document.write(" 
     "); 
} 
} 
</script> 

回答

1

XML文件路径设置不正确,尝试

echo '<option value="XML/'.$area.'">'.$area.'</option>'; 

而且你有一个多行字符串,这将导致一个语法错误,更改

document.write(" 
    "); 

喜欢的东西

document.write("\n"); 

另外我们在加载页面后会覆盖整个页面。

+0

进行了所有您指出的更改,但不幸的是它仍然无法正常工作! – 6TTW014

+0

@ Kimmy25你有什么错误,如果你提醒(xhttp.status),你会得到什么;'' – Musa