PHP Example - AJAX RSS Reader
An RSS reader is used to read RSS feeds.
AJAX RSS Reader
In the example below, we will demonstrate an RSS reader through which content from RSS is loaded into the web page without it being refreshed:
RSS-feed data list...
Example Explanation - HTML Page
When the user selects an RSS feed in the dropdown list above, a function named "showRSS()" is executed. This function is triggered by the "onchange" event:
Example
<html>
<head>
<meta charset="utf-8">
<title>Runoob Tutorial(runoob.com)
title
<
script
function showRSS(str) { if (str.length==0) { document.getElementById("rssOutput").innerHTML=""; return; } if (window.XMLHttpRequest) { // IE7+, Firefox, Chrome, Opera, Safari browser execution code xmlhttp=new XMLHttpRequest(); } else { // IE6, IE5 browser execution code xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { document.getElementById("rssOutput").innerHTML=xmlhttp.responseText; } } xmlhttp.open("GET","getrss.php?q="+str,true); xmlhttp.send(); }
script
head
<body>
<form>
<select onchange="showRSS(this.value)">
<option value="">Select an RSS feed:
option
<option value="rss">Read RSS Data
option
select
form
<br>
<div id="rssOutput">RSS-feed data list...
div
body
html
The showRSS() function performs the following steps:
- Check whether an RSS feed is selected
- Create an XMLHttpRequest object
- Create the function to be executed when the server response is ready
- Send a request to the file on the server
- Note the parameter (q) appended to the end of the URL (containing the dropdown list contents)
PHP File
file rss_demo.xml.
The server page called via JavaScript above is a PHP file named "getrss.php":
Example
load ( $xml ) ; // from " " . $channel_title . " " ) ; echo ( "
" ) ; echo ( $channel_desc . "
" . $item_title . " " ) ; echo ( "
" ) ; echo ( $item_desc . "
When the RSS feed request is sent from JavaScript to the PHP file, the following happens:
- Check which RSS feed is selected
- Create a new XML DOM object
- Load the RSS document into the xml variable
- Extract and output elements from the channel element
- Extract and output elements from the item element