0

我正在尝试根据单选按钮选择的特定值在从 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>
4

0 回答 0