We now have a youtube channel. Subscribe!



Hello folks! welcome back to a new edition 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 an xml from the local directory and also the server. The following example below demonstrates 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 calls the getcourse.php using the GET method. The 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 a list of courses and details. The file can be accessed by the 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 we 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.

Post a Comment

Hello dear readers! Please kindly try your best to make sure your comments comply with our comment policy guidelines. You can visit our comment policy page to view these guidelines which are clearly stated. Thank you.
© 2023 ‧ WebDesignTutorialz. All rights reserved. Developed by Jago Desain