Hello dear readers! welcome back to another section of our tutorial on PHP. In this tutorial guide, we will be discussing about the PHP AJAX XML Parser.

AJAX XML Example

Using PHP with AJAX, we can parse xml from the local directory as well as servers. The following example below illustrates how to parse xml with browser.

         function showCD(str) {
            if (str == "") {
               document.getElementById("txtHint").innerHTML = "";
            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("txtHint").innerHTML = xmlhttp.responseText;
         Select a Course:
         <select name = "cds" onchange = "showCD(this.value)">
            <option value = "">Select a course:</option>
            <option value = "Android">CSS</option>
            <option value = "Html">HTML</option>
            <option value = "Java">JavaScript</option>
            <option value = "Microsoft">Python</option>
      <div id = "txtHint"><b>Course info will be listed here...</b></div>

The above example will call the getcourse.php by using the GET method. getcourse.php file loads catalog.xml. getcourse.php file is shown below -

   $q = $_GET["q"];
   $xmlDoc = new DOMDocument();
   $x = $xmlDoc->getElementsByTagName('COURSE');
   for ($i = 0; $i<=$x->length-1; $i++) {
      if ($x->item($i)->nodeType == 1) {
         if ($x->item($i)->childNodes->item(0)->nodeValue == $q) {
            $y = ($x->item($i)->parentNode);
   $cd = ($y->childNodes);
   for ($i = 0;$i<$cd->length;$i++) {
      if ($cd->item($i)->nodeType == 1) {
         echo("<b>" . $cd->item($i)->nodeName . ":</b> ");


This is the xml file having list of courses and details. This file is accessed by getcourse.php -

      <COMPANY>Web Design Tutorialz</COMPANY>
      <COMPANY>Web Design Tutorialz</COMPANY>
      <COMPANY>Web Design Tutorialz</COMPANY>
      <COMPANY>Web Design Tutorialz</COMPANY>


When the above code is executed, it will produce the following result -

Alright guys! This is where we are rounding up for this tutorial post. In our next tutorial guide, we are going to be discussing about the AJAX Auto Complete Search in PHP.

Feel free to ask your questions where necessary and i will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.