Ajax with XML

Ajax with XML

Posted in : PHP Posted on : February 1, 2011 at 5:21 PM Comments : [ 0 ]

This section contains the detail about the Ajax with XML using PHP.

Ajax with XML

In this section, you will learn how you can create a web page which can communicate with an XML page using Ajax and PHP.


In this example, the values stored in the XML file award.xml is fetched by the PHP script written in AjaxXmlMain.php and will display on the onchange event of java script. The DOM parser is user by the PHP script to parse the XML file. For more details on parsing Click here. You can find the PHP XML section there. This whole section is for XML File handling through PHP.

Given below the client side code:


<script type="text/javascript">
function displaySelected(strg)
if (strg=="")
if (window.XMLHttpRequest)
xmlhttp=new XMLHttpRequest();
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
if (xmlhttp.readyState==4 && xmlhttp.status==200)
<h3><i><font color='blue'>BHARAT RATNA AWARD WINNERS</font></i></h3>
Please Select an Option:
<select name="winner" onchange="displaySelected(this.value)">
<option value="">Select a Bharat Ratna Winner</option>
<option value="C. V. Raman">C. V. Raman</option>
<option value="Jawaharlal Nehru">Jawaharlal Nehru</option>
<option value="Rajendra Prasad">Rajendra Prasad</option>
<option value="Lal Bahadur Shastri">Lal Bahadur Shastri</option>
<option value="Mother Teresa">Mother Teresa</option>
<option value="Vinoba Bhave">Vinoba Bhave</option>
<option value="Satyajit Ray">Satyajit Ray</option>
<option value="J. R. D. Tata">J. R. D. Tata</option>
<div id="detail">
<b>The Details of the selected Bharat Ratna will be display here.....</b>

The server side PHP script is given below :



$xmlDoc = new DOMDocument();


for ($i=0; $i<=$b->length-1; $i++)
//Process only element nodes
if ($b->item($i)->nodeType==1)
if ($b->item($i)->childNodes->item(0)->nodeValue == $s)


for ($i=0;$i<$winner->length;$i++)
//Process only element nodes
if ($winner->item($i)->nodeType==1)
echo("<b>" . $winner->item($i)->nodeName . ":</b> ");
echo("<br />");

The XML file award.xml is given below :

<?xml version="1.0" encoding="ISO-8859-1"?>
<NAME>C. V. Raman</NAME>
<NAME>Jawaharlal Nehru</NAME>
<FIELD>Independence activist, author, first Prime Minister</FIELD>
<NAME>Rajendra Prasad</NAME>
<FIELD>Independence activist, jurist, first President</FIELD>
<NAME>Lal Bahadur Shastri</NAME>
<FIELD>Posthumous, independence activist, second Prime Minister</FIELD>
<NAME>Mother Teresa</NAME>
<FIELD>Catholic nun, founder of the Missionaries of Charity</FIELD>
<NAME>Vinoba Bhave</NAME>
<FIELD>Posthumous, social reformer, independence activist</FIELD>
<NAME>Satyajit Ray</NAME>
<FIELD>Bengali filmmaker</FIELD>
<NAME>J. R. D. Tata</NAME>
<FIELD>Industrialist and philanthropist</FIELD>


At initial, it looks like:

When you select a person name from drop down list, it will appear like this :

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image

Tutorial Topics