2011-12-10 30 views
1

因此,我使用PHP來解析XML文件並將標題顯示到不同的故事。我有一個選擇菜單來過濾基於公司的故事。我有這個工作,但它刷新頁面。有人能給我提供一個如何通過Ajax做到這一點的例子,因此整個頁面不會刷新?任何幫助表示讚賞。jQuery更新PHP var在選擇菜單上的值變化

活生生的例子:http://recoverstudio.com/test/

當前代碼:

<form name="feed-form" action="" method="GET"> 
    <select name="feed" onchange="this.form.submit();"> 
    <option value="">Filter</option> 
    <option value="https://www.pitchengine.com/Feed/AgencyRss/036d17f9-50ff-4cf1-9273-8865ba82d9ff">All</option> 
    <option value="http://pitchengine.com/Feed/BrandRss/cf6f4ba5-5e43-47c8-a7f2-1a9cb490c639">Brand 1</option> 
    </select> 
</form> 

<div id="press-releases"> 
    <div id="pitch-engine"> 
    <?php 
     $url = 'https://www.pitchengine.com/Feed/AgencyRss/036d17f9-50ff-4cf1-9273-8865ba82d9ff'; 
     if(isset($_GET['feed'])){ 
      $url = $_GET['feed']; 
     } 
     $xml = simplexml_load_file($url); 

     echo '<table>'; 
     echo '<tr class="head"><td>Date</td><td>Title</td></tr>'; 
     foreach($xml->channel->item as $item) 
     { 
      echo '<tr>'; 
      echo '<td>' . $item->pubDate . '</td>'; 
      echo '<td><a href="' . $item->link . '">' . $item->title . '</a></td>'; 
      echo '</tr>'; 
     } 
     echo '</table>'; 
    ?> 
    </div> 

回答

0

基本上你想對jQuery的.change事件AJAX調用。

像這樣的東西應該這樣做(未經測試):

$('#myselect').change(function() { 
    var url = $(this).find('option:selected').val(); 
    $.ajax({ 
     url:url, 
     type:'GET', 
     success: function (res) { 
      // res should contain your XML string - parse it 
     } 
     error: function (xhr, status, errorThrown) { 
      console.log(status+' - '+errorThrown); 
      alert('Something went wrong processing the request.'); 
     } 
    }); 
}); 

你或許可以理清XML解析自己 - 我只是有經驗的JavaScript解析JSON。