【问题标题】:Get selected value text from dropdown with XML AND PHP使用 XML 和 PHP 从下拉列表中获取选定的值文本
【发布时间】:2020-07-20 20:48:51
【问题描述】:

我有下一个代码:

<form method="POST">
<?php
 echo "<select value>";
 $xml = new SimpleXMLElement("http://news.google.com/news?ned=us&topic=h&output=rss",null,true);
   foreach($xml->channel->item as $news)
{
   echo "<option value='".$news->title."'>" . $news->title . "</option>";
   }
   echo "</select>";
 ?>
</form>

所以我有一个带有新闻标题的下拉框,当我选择一个标题时,我想显示 xml 文件中的描述,但我不知道该怎么做,如果有人可以帮助我,请

【问题讨论】:

  • 你最好用 xpath 做一些事情来找到相应的故事——比如//item[title="optionSelected"]
  • 在我看来你必须使用 jquery。例如。 jQuery.parseXML()

标签: php html xml


【解决方案1】:

请查看此示例,如果您需要更多帮助,请告诉我。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
    $(document).ready(function(){
        $(".feeds").change(function(){
        var val=$(this).val();
            $(".common").hide();
            $("."+val).show();
        });
    });
</script>
<style>
    .common{display:none;}
</style>

<?php
$xml = new SimpleXMLElement("http://news.google.com/news?ned=us&topic=h&output=rss",null,true);
$select_html="<select class='feeds'>";
$d_html="";

foreach($xml->channel->item as $news){
    $d_html.="<div class='".$news->guid." common'>".$news->description.'</div>';
    $select_html.="<option value='".$news->guid."'>" . $news->title . "</option>";
}
$select_html.="</select>";

echo $select_html;
echo $d_html;
?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-07
    • 2018-05-04
    • 2010-12-11
    相关资源
    最近更新 更多