繁体   English   中英

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

[英]Show rest XML file item attributes based on dropdown selection

我有一个 XML 文件。 我需要选择“Grad”来显示来自 Firma 和 BrojTelefona 属性的值,位于选项列表右侧的某个位置。 作为文字。

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>

我的 XML 文件:

<?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>

我找到了解决方案。

 <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>

和 get.php 文件

<?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>");
  }
}
?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM