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