PHP development basic tutorial AJAX RSS Reader
AJAX RSS Reader
RSS Reader is used to read RSS Feeds.
Example:
In the following example, we will demonstrate an RSS reader, through which the content from RSS is read without refreshing the web page. Loading:
This example includes three parts
HTML form page
PHP file
XML file
##HTML form page
When the user selects an RSS-feed in the drop-down list above, a function named "showRSS()" is executed. This function is triggered by the "onchange" event: See 1.php for the source codephp中文网(php.cn)
The form works like this:
- When the user selects an option in the drop-down box, an event is triggered
- When the event is triggered, execute the showRSS() function
- Below the form is a named "rssOutput". It is used as a placeholder for the data returned by the showRSS() function. The showRSS() function will perform the following steps:
- Check whether an RSS-feed is selected
- Create an XMLHttpRequest object
- Create a function that is executed when the server response is ready
- Send a request to a file on the server
- Please note the parameter (q) added to the end of the URL (containing the contents of the drop-down list)
PHP file
The server page called above through JavaScript is a PHP file named "2.php": See 2.phpload($xml); // 从 "
for the source code When a request for an RSS feed is sent from JavaScript to a PHP file, what happens is:" 中读取元素 $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; // 输出 " " 中的元素 echo(" " . $channel_title . ""); echo("
"); // 输出 "
"); echo($channel_desc . "- " 中的元素 $x=$xmlDoc->getElementsByTagName('item'); for ($i=0; $i<=1; $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; $item_desc=$x->item($i)->getElementsByTagName('description') ->item(0)->childNodes->item(0)->nodeValue; echo ("
" . $item_title . ""); echo ("
"); } ?>
"); echo ($item_desc . "- Check which RSS feed is selected
- Create a new one XML DOM object
- Load RSS document in xml variable
- Extract and output elements from channel element
- Extract and output elements from item elements
XML filephp教程 //m.sbmmt.com学的不仅技术,更新梦想!! RSS 教程 //m.sbmmt.com/rss/rss-tutorial.html通过使用 RSS,您可以有选择地浏览您感兴趣的以及与您的工作相关的新闻 XML 教程 //m.sbmmt.com/xml/xml-tutorial.htmlXML 指可扩展标记语言(eXtensible Markup Language)
Learning experience
This example mainly includes the following knowledge points:- Form basics
- onkeyup event: when the keyboard key is pressed Occurs when released
- Function call, function value transfer
- Creation of AJAX XMLHttpRequest object, function executed when the server responds, and File sending request on the server: see 1-5 for learning experience
HTML DOM getElementById() method: Returns a reference to the first object with the specified ID
XML related knowledge
Create an XML DOM object
Load the XML file into a new XML DOM object
Get the object with a specific tag name: getElementsByTagName ()
Returns the first child node of the element: HTML DOM item() method