简体   繁体   中英

Get selected value text from dropdown with XML AND PHP

I have the next code:

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

So i have a dropdown box with news titles, when i select a title i want to appear the description from the xml file and i dont know how to do it, if someone can help me, please

Please check this example and let me know if you want any more help.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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