我试图在基于单选按钮选择的特定值从XML文件生成的表中搜索数据。我已经设法让每个搜索在一个单独的php文件上独立工作,但我想在可能的情况下从我的主页运行它。我的基本代码如下:
<div >
<input type="text" style="float:left">
<button type="button">Search</button>
<form action="">
<input type="radio" name="search" value="id">ID<br>
<input type="radio" name="search" value="sender">Sender<br>
<input type="radio" name="search" value="recipient">Recipient<br>
<input type="radio" name="search" value="date">Date<br>
</form>
</div>
</h2>
<body background="background.jpg" style="background-size:100% 100%; no-repeat scroll; min-height: 700px;">
<script>
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","memos.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
document.write("<table border='1' bgcolor='white' align='center' style='margin-top:200px; margin-left:250px;'>");
var x=xmlDoc.getElementsByTagName("memo");
document.write("<th>Sender</th>");
document.write("<th>Subject</th>");
document.write("<th>Recipient</th>");
document.write("<th>Message</th>");
document.write("<th>Date</th>");
for (i=0;i<x.length;i++)
{
document.write("<tr><td>");
//document.write(x[i].getElementsByTagName("id")[0].childNodes[0].nodeValue);
//document.write("</td><td>");
document.write(x[i].getElementsByTagName("sender")[0].childNodes[0].nodeValue);
document.write("</td><td>");
document.write(x[i].getElementsByTagName("title")[0].childNodes[0].nodeValue);
document.write("</td><td>");
document.write(x[i].getElementsByTagName("recipient")[0].childNodes[0].nodeValue);
document.write("</td><td>");
document.write(x[i].getElementsByTagName("body")[0].childNodes[0].nodeValue);
document.write("</td><td>");
document.write(x[i].getElementsByTagName("date")[0].childNodes[0].nodeValue);
document.write("</td></tr>");
}
document.write("</table>");
另外,您可能会从代码中注意到我在显示xml文件中的ID元素时遇到问题。如果有人能够帮助我,我也非常感激。 XML doc的基本结构是:
<database>
<nextID>2</nextID>
<memos>
<memo id="1">
<title>Here We Go!</title>
<sender>Joe</sender>
<recipients>
<recipient>You</recipient>
<recipient>Him</recipient>
<recipient>Her</recipient>
</recipients>
<date>2013-10-09</date>
<body>Let's do this!</body>
</memo>
</memos>
</database>