![]() Join Up! 96813 members and counting! |
|
|||
AJAX and PHP Part 2 -- XML Communication/Processing
Jon Campbell
AJAX and PHP 5 both have powerful features for processing and using an XML document. XML is a method of formatting data often for communication purposes between different computer systems. In this article, we will show you how to access an XML document with AJAX!
Basically the AJAX client-browser and the PHP server can communicate any kind of data (except images and binary) by means of XML documents. Part 1 of this series on AJAX and PHP showed the basics of AJAX communication, and if you are not familiar with AJAX you should read Part 1. This article will focus on processing requested XML data within the client-browser, and we will touch on the PHP server side XML processing as well. This tutorial will primarily focus on the client-browser use of XML received from a server by means of AJAX; this is the most common use of XML with AJAX. Sending requests to a PHP server script is usually in the form of a GET or POST method, but for a client-browser to receive many fields of organized information, one can use XML as we will demonstrate.
You can download the complete working example of this tutorial here.
PHP Script Handling of XML
The following example script could be saved in a file called getxml.php or what ever you want to name it, this would read an XML document file called somedoc.xml and print the XML as a response to an AJAX request. Our AJAX example will use the following PHP script and the XML data following that in Listing B. Note that any PHP script producing XML data must set the response header to an XML type, as we have done with the header( ) statement in Listing A. This PHP script simply reads the XML file and prints it to the browser.
Listing A: PHP script 'getxml.php'
Using PHP you can also access XML elements of DOM object variables, the $dom variable in this case, with PHP commands like the following:
Listing B: XML file 'somedoc.xml' contents
AJAX Processing of XML
AJAX is an advanced feature of JavaScript primarily programmed within client-browser pages; any server-side programmable language can support it. JavaScript has what is called the DOM or Document Object Model for programming--it is used for XML processing. We will use the DOM methods .getElementsByTagName( ) and .childNodes[ ].value in this tutorial, as these are the methods that allow the client-browser to process XML documents that are received via AJAX.
Listing C: HTML/JavaScript page utilizing XML requests
Our HTML/JavaScript page above could be saved as index.html or mypage.html or it could even be a PHP page. The AJAX features are all standard but you should notice the following line within the getHttpRes( ) function above. This gets the AJAX response from the server into the xmlres variable, but as an XML document, not as just Text.
The xmlres variable becomes a type XML DOM object. This xmlres variable is passed to the processXML( ) function which processes the various tags and contents in the XML document, examine this function.
Notice the 2nd line document.getElementById(cid).innerHTML, this does dynamic updating of the cells of our HTML table using the <title> and <description> fields of the XML document variable passed to the above function as xmlDoc. You can break up an XML document into a set of elements as we do in this line of our above function: var cells = xmlDoc.getElementsByTagName("cell"); This creates another XML document variable named cells which holds all the "cell" elements of the original XML document shown in Listing B.
We hope you have enjoyed the topic and found this tutorial educational. Look for more tutorials in this AJAX and PHP series of articles coming soon!
|