php - 根据下拉选择显示其余 XML 文件项属性
问题描述
我有一个 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>
并获取.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>");
}
}
?>
推荐阅读
- powershell - 将数据写入 csv 并导出到现有 excel 时出错
- android - 如何在 C++ Builder 中动态创建 Java 对象?
- image - 如何将自定义元数据放入 docker 图像中?
- javascript - 使用 PEG.js 使用 JavaScript 解析表达式
- javascript - 我想在 Vue JS 的服务层中隐藏 Spinner 功能
- c# - 找不到文件或程序集“System.Runtime, Version=4.1.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a”或对它的依赖
- google-analytics - 如何将 Shopify 报告为 GA 从总定价/收入更改为净定价/收入
- git - 对合并的分支进行变基
- qt - 如何在 QLineEdit 中设置插入符号闪烁光标的颜色
- redhat - yum list 没有列出 repo 中存在的所有包