PHP and AJAX XML Example

In the AJAX example below we will demonstrate how a web page can fetch information from an XML file using AJAX technology.

This example consists of four pages:
  • a simple HTML form
  • an XML file
  • a JavaScript
  • a PHP page

The HTML Form

The example above contains a simple HTML form and a link to a JavaScript:

<script src="selectcd.js"></script>
Select a CD:
<select name="cds" onchange="showCD(this.value)">
<option value="Bob Dylan">Bob Dylan</option>
<option value="Bee Gees">Bee Gees</option>
<option value="Cat Stevens">Cat Stevens</option>
<div id="txtHint"><b>CD info will be listed here.</b></div>

Example Explained

As you can see it is just a simple HTML form  with a simple drop down box called "cds".
The paragraph below the form contains a div called "txtHint". The div is used as a placeholder for info retrieved from the web server.
When the user selects data, a function called "showCD" is executed. The execution of the function is triggered by the "onchange" event.
In other words: Each time the user changes the value in the drop down box, the function showCD is called.

The XML File

The XML file is "cd_catalog.xml". This document contains a CD collection.

The JavaScript

This is the JavaScript code stored in the file "selectcd.js":

var xmlHttp;

function showCD(str)
if (xmlHttp==null)
 alert ("Browser does not support HTTP Request");
var url="getcd.php";

function stateChanged() 
 if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
function GetXmlHttpObject()
var xmlHttp=null;
 // Firefox, Opera 8.0+, Safari
 xmlHttp=new XMLHttpRequest();
catch (e)
 // Internet Explorer
  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
 catch (e)
  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
return xmlHttp;

Example Explained

The stateChanged() and GetXmlHttpObject functions are the same as in the last chapter, you can go to the previous page for an explanation of those

The showCD() Function
If an item in the drop down box is selected the function executes the following:
  1. Calls on the GetXmlHttpObject function to create an XMLHTTP object
  2. Defines the url (filename) to send to the server
  3. Adds a parameter (q) to the url with the content of the input field
  4. Adds a random number to prevent the server from using a cached file
  5. Call stateChanged when a change is triggered
  6. Opens the XMLHTTP object with the given url.
  7. Sends an HTTP request to the server

The PHP Page

The server paged called by the JavaScript, is a simple PHP file called "getcd.php".
The page is written in PHP using the XML DOM to load the XML document "cd_catalog.xml".
The code runs a query against the XML file and returns the result as HTML:

$xmlDoc = new DOMDocument();
for ($i=0; $i<=$x->length-1; $i++)
//Process only element nodes
if ($x->item($i)->nodeType==1)
  if ($x->item($i)->childNodes->item(0)->nodeValue == $q)
for ($i=0;$i<$cd->length;$i++)
//Process only element nodes
if ($cd->item($i)->nodeType==1)
  echo(": ");
  echo("<br />");

Example Explained

When the query is sent from the JavaScript to the PHP page the following happens:
  1. PHP creates an XML DOM object of the "cd_catalog.xml" file
  2. All  "artist" elements (nodetypes = 1) are looped through to find a name matching the one sent from the JavaScript.
  3. The CD containing the correct artist is found
  4. The album information is output and sent to the "txtHint" placeholder
PHP and AJAX XML Example Reviewed by 1000sourcecodes on 22:58 Rating: 5
Powered by Blogger.