---
title: "Using AJAX with XML in PHP"  
description: "AJAX can be used for interactive communication with an XML. In this article I will show you, how to read an xml file using with AJAX.  Let’s have an e"  
author: "Anonymous User"  
published: 2011-09-27  
updated: 2020-03-04  
canonical: https://www.mindstick.com/articles/746/using-ajax-with-xml-in-php  
category: "php"  
tags: ["php"]  
reading_time: 2 minutes  

---

# Using AJAX with XML in PHP

AJAX can be used for [interactive](https://answers.mindstick.com/qa/38931/who-has-won-the-2016-fifa-interactive-world-cup-fiwc) [communication](https://www.mindstick.com/articles/126321/5-fails-and-fixes-of-the-office-communication) with an XML. In this [article](https://yourviews.mindstick.com/view/81489/kashmir-now-after-an-year-of-abrogation-of-article-370) I will show you, how to read an [xml file](https://www.mindstick.com/articles/75/how-to-read-and-write-xml-file-through-c-sharp) using with AJAX.\

Let’s have an example, how to read [links](https://www.mindstick.com/blog/415/css3-links) from xml file using with AJAX in PHP.

##### Example:

Here I am creating [simple](https://www.mindstick.com/blog/63525/get-the-most-out-of-maths-with-these-8-simple-tricks) [web page](https://answers.mindstick.com/qa/92898/how-to-create-a-web-page-using-bootstrap-4) UI which contains one text box, when we enter a [character](https://www.mindstick.com/articles/23551/an-investigate-distinctive-seafood-restaurant-for-your-image-stamp-character) into [textbox](https://www.mindstick.com/articles/320/textbox-control-in-vb-dot-net) then this character will be searched into xml file and appropriate link will be returned to the [web browser](https://answers.mindstick.com/qa/95505/what-are-the-pros-and-cons-of-using-safari-web-browser).

##### Coding of ‘LiveSearchAjax.php’:

```
<!DOCTYPE html><html>     <head>         <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">         <title>Live Search content with AJAX</title>         <script type="text/javascript">             function showLivSearch(str) {                 if (str.length==0) {                    document.getElementById("liveSuggest").innerHTML= "";                    document.getElementById("liveSuggest").style.border ="0px";                     return;                }                if (window.XMLHttpRequest) {                    xmlHttp=newXMLHttpRequest();                }                  xmlHttp.onreadystatechange=function () {                     if (xmlHttp.readyState==4) {                        document.getElementById("liveSuggest").innerHTML= xmlHttp.responseText;                        document.getElementById("liveSuggest").style.border ="1px solid Black";                    }                }                xmlHttp.open("GET", "LiveSearchContent.php?qryVal="+str, true);                xmlHttp.send();            }</script>  </head>     <body>         <table>             <tr>                 <td>                     <p style="background-color: gold;width: 200px;" > Type something here...</p>                 </td>                 <td>                     <input type="text" id="livSearch" onkeyup="javascript: showLivSearch(this.value);"></input>                 </td>             </tr>              </table>                 <div id ="liveSuggest">                     </div>     </body></html>
```

![Using AJAX with XML in PHP](https://www.mindstick.com/mindstickarticle/c2cbc944-67bd-4253-aa1a-d17cfc002733/images/5c232269-a54e-477c-bf41-cc23c1c2818e.png)

Now, when you type anything into text box then ‘LiveSearchContent.php’ script will be called and return the appropriate links of that character or string.

##### Coding of ‘LiveSearchContent.php’:

```
<?php    //  CreateDOMDocument instance         $xmlDoc= new DOMDocument();    //  load XML file    $xmlDoc->load("C://Users//Sachindra//Desktop//liveSearch.xml");    // get the parenet tag name    $ptag = $xmlDoc->getElementsByTagName('link');    // get the parameter value of page url     $val = $_GET['qryVal'];      //lookup all links from the xml file if length of val>0    if (strlen($val)>0)    {    $hint="";       for($i=0; $i<($ptag ->length); $i++)      {        // find out the child node         $child1=$ptag->item($i)->getElementsByTagName('title');        $child2=$ptag->item($i)->getElementsByTagName('url');          // check the child node condition      if ($child1->item(0)->nodeType==1)       {        //find a link matching the search text        if (stristr($child1->item(0)->childNodes->item(0)->nodeValue,$val))          {            // check if no hints is available           if ($hint=="")            {              // open the link in new tab            $hint="<a href='" .           $child2->item(0)->childNodes->item(0)->nodeValue .           "'target='_blank'>" .           $child1->item(0)->childNodes->item(0)->nodeValue . "</a>";            }          else            {        $hint=$hint . "<br /><a href='" .            $child2->item(0)->childNodes->item(0)->nodeValue .            "'target='_blank'>" .            $child1->item(0)->childNodes->item(0)->nodeValue . "</a>";           }          }        }      }    }      // Set output to no suggestion available    if ($hint=="")      {       $response="No suggestion";      }    else      {       $response=$hint;      }      //diaplay the output response    echo $response;?>
```

Here I am using ‘liveSearch.xml’ file. The structure of ‘liveSearch.xml’ file:

![Using AJAX with XML in PHP](https://www.mindstick.com/mindstickarticle/c2cbc944-67bd-4253-aa1a-d17cfc002733/images/e035bf98-697e-4ffe-a8cf-eb3f134221d7.png)

##### Output:

![Using AJAX with XML in PHP](https://www.mindstick.com/mindstickarticle/c2cbc944-67bd-4253-aa1a-d17cfc002733/images/e41727b7-e62f-4bcf-9063-5ec35d4d162d.png)

\

---

Original Source: https://www.mindstick.com/articles/746/using-ajax-with-xml-in-php

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
