Está en la página 1de 3

PHP Example AJAX RSS Reader

Page 1 of 3

TRANSLATE
Search w3schools.com
HOME HTML CSS XML JAVASCRIPT ASP PHP SQL MORE...

Search

REFERENCES | EXAMPLES | FORUM | ABOUT

TOP 100: Free Website Templates Free CSS Templates Free HTML Templates Free Web Templates

ASP.NET,C# Free Training Over 100s of Videos on C#, ASP.NET Training. Please Visit www.dotnetvideos.net Free Online Advertising See What $75 of Google Ads Can Do For Your Business. Try It Now! www.Google.com/AdWords SQL Server Query Tool Improve SQL Server Performance 65%, Save on Costs, Free Trial Download! www.Confio.com/SQL-Server-Query

PHP Basic
PHP HOME PHP Intro PHP Install PHP Syntax

PHP Example - AJAX RSS Reader


Previous
An RSS Reader is used to read RSS Feeds.

WEB HOSTING Best Web Hosting

Next Chapter

PHP MySQL Hosting Best Hosting Coupons UK Reseller Hosting Cloud Hosting Top Web Hosting $3.98 Unlimited Hosting Premium Website Design WEB BUILDING XML Editor - Free Trial!

PHP Variables PHP String PHP Operators PHP If...Else PHP Switch PHP Arrays PHP While Loops PHP For Loops PHP Functions PHP Forms PHP $_GET PHP $_POST

AJAX RSS Reader


The following example will demonstrate an RSS reader, where the RSS-feed is loaded into a webpage without reloading:

Select an RSS-feed:
RSS-feed will be listed here...

FREE Website BUILDER Free Website TemplatesFree CSS Templates Make Your Own Website W3SCHOOLS EXAMS Get Certified in: HTML, CSS, JavaScript, XML, PHP, and ASP W3SCHOOLS BOOKS New Books: HTML, CSS JavaScript, and Ajax STATISTICS Browser Statistics Browser OS Browser Display SHARE THIS PAGE Share with

Example Explained - The HTML Page


When a user selects an RSS-feed in the dropdown list above, a function called "showResult()" is executed. The function is triggered by the "onchange" event:

PHP Advanced
PHP Date PHP Include PHP File PHP File Upload PHP Cookies PHP Sessions PHP E-mail PHP Secure E-mail PHP Error PHP Exception PHP Filter

< html> < head> < script type="text/javascript"> function showRSS(str) { if (str.length==0) { document.getElementById("rssOutput").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 (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>

PHP Database
MySQL Introduction MySQL Connect MySQL Create MySQL Insert MySQL Select MySQL Where MySQL Order By MySQL Update MySQL Delete PHP ODBC

PHP XML
XML Expat Parser XML DOM XML SimpleXML

PHP and AJAX


AJAX Intro AJAX PHP AJAX Database AJAX XML AJAX Live Search AJAX RSS Reader

http://www.w3schools.com/php/php_ajax_rss_reader.asp

3/1/2012

PHP Example AJAX RSS Reader

Page 2 of 3

AJAX Poll

< select onchange="showRSS(this.value)"> < option value="">Select an RSS-feed:</option> < option value="Google">Google News</option> < option value="MSNBC">MSNBC News</option> < /select> < /form> < br /> < div id="rssOutput">RSS-feed will be listed here...</div> < /body> < /html>
The showResult() function does the following: Check if an RSS-feed is selected Create an XMLHttpRequest object Create the function to be executed when the server response is ready Send the request off to a file on the server Notice that a parameter (q) is added to the URL (with the content of the dropdown list)

PHP Reference
PHP Array PHP Calendar PHP Date PHP Directory PHP Error PHP Filesystem PHP Filter PHP FTP PHP HTTP PHP Libxml PHP Mail PHP Math PHP Misc PHP MySQL PHP SimpleXML PHP String PHP XML PHP Zip

The PHP File


The page on the server called by the JavaScript above is a PHP file called "getrss.php":

PHP Quiz
PHP Quiz PHP Certificate

< ?php //get the q parameter from URL $q=$_GET["q"]; //find out which feed was selected if($q=="Google") { $xml=("http://news.google.com/news?ned=us&topic=h&output=rss"); } elseif($q=="MSNBC") { $xml=("http://rss.msnbc.msn.com/id/3032091/device/rss/rss.xml"); } $xmlDoc = new DOMDocument(); $xmlDoc->load($xml); //get elements from "<channel>" $channel=$xmlDoc->getElementsByTagName('channel')->item(0); $channel_title = $channel->getElementsByTagName('title') ->item(0)->childNodes->item(0)->nodeValue; $channel_link = $channel->getElementsByTagName('link') ->item(0)->childNodes->item(0)->nodeValue; $channel_desc = $channel->getElementsByTagName('description') ->item(0)->childNodes->item(0)->nodeValue; //output elements from "<channel>" echo("<p><a href='" . $channel_link . "'>" . $channel_title . "</a>"); echo("<br />"); echo($channel_desc . "</p>"); //get and output "<item>" elements $x=$xmlDoc->getElementsByTagName('item'); for ($i=0; $i<=2; $i++) { $item_title=$x->item($i)->getElementsByTagName('title') ->item(0)->childNodes->item(0)->nodeValue; $item_link=$x->item($i)->getElementsByTagName('link') ->item(0)->childNodes->item(0)->nodeValue;

http://www.w3schools.com/php/php_ajax_rss_reader.asp

3/1/2012

PHP Example AJAX RSS Reader

Page 3 of 3

$item_desc=$x->item($i)->getElementsByTagName('description') ->item(0)->childNodes->item(0)->nodeValue; echo ("<p><a href='" . $item_link . "'>" . $item_title . "</a>"); echo ("<br />"); echo ($item_desc . "</p>"); } ?>
When a request for an RSS feed is sent from the JavaScript, the following happens: Check which feed was selected Create a new XML DOM object Load the RSS document in the xml variable Extract and output elements from the channel element Extract and output elements from the item elements

Previous

Next Chapter

apple.com

REPORT ERROR | HOME |

TOP | PRINT |

FORUM |

ABOUT

W3Schools is optimized for learning, testing, and training. Examples might be simplified to improve reading and basic understanding. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using this site, you agree to have read and accepted our terms of use and privacy policy. Copyright 1999-2012 by Refsnes Data. All Rights Reserved.

http://www.w3schools.com/php/php_ajax_rss_reader.asp

3/1/2012

También podría gustarte