dongxi7704 2019-02-08 15:03
浏览 90

根据下拉选择显示其余XML文件项属性

I have an XML file. I need on "Grad" selection to show values from Firma and BrojTelefona attributes, somewhere on the right side of Option list. As text.

HTML:

<html>
<form method="post" action="">
    <?php
    echo "<select>";
        $xml = simplexml_load_file('adler.xml');
            foreach ($xml->record as $item)
            {
                 echo "<option value='".$item->Grad."'>" . $item->Grad . "</option>";
            }
    echo "</select>";
    ?>
</form>
</html>

My XML file:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<data-set xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <record>
        <Firma>TD electronic</Firma>
        <BrojTelefona>034 715 445</BrojTelefona>
        <Grad>Arandjelovac</Grad>
    </record>
    <record>
        <Firma>Lihno d.o.o.</Firma>
        <BrojTelefona>011 848 5705</BrojTelefona>
        <Grad>Batajnica</Grad>
    </record>
  • 写回答

1条回答 默认 最新

  • duanluo5096 2019-02-08 15:35
    关注

    I have found solution.

     <html>
    <head>
    <script>
    function show(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=function() {
        if (this.readyState==4 && this.status==200) {
          document.getElementById("txtHint").innerHTML=this.responseText;
        }
      }
      xmlhttp.open("GET","get.php?q="+str,true);
      xmlhttp.send();
    }
    </script>
    </head>
    <body>
    <form>
        <?php
         echo '<select onchange="show(this.value)">';
            $xml = simplexml_load_file('adler.xml');
                foreach ($xml->record as $item)
                {
                     echo "<option value='".$item->Grad."'>" . $item->Grad . "</option>";
                }
        echo "</select>";
        ?>
    </form>
    <div id="txtHint"><b>Servis Adler uređaja za </b></div>
    </body>
    </html>
    

    And get.php file

    <?php
     $q=$_GET["q"];
    
     $xmlDoc = new DOMDocument();
     $xmlDoc->load("adler.xml");
    
    $x=$xmlDoc->getElementsByTagName('Grad');
    
    for ($i=0; $i<=$x->length-1; $i++) {
      //Process only element nodes
      if ($x->item($i)->nodeType==1) {
        if ($x->item($i)->childNodes->item(0)->nodeValue == $q) {
          $y=($x->item($i)->parentNode);
        }
      }
    }
    
    $record=($y->childNodes);
    
    for ($i=0;$i<$record->length;$i++) { 
      //Process only element nodes
      if ($record->item($i)->nodeType==1) {
        echo("<b>" . $record->item($i)->nodeName . ":</b> ");
        echo($record->item($i)->childNodes->item(0)->nodeValue);
        echo("<br>");
      }
    }
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看