AJAX XML

From http://www.w3schools.com (Copyright Refsnes Data)

# AJAX XML Example

AJAX can be used for interactive communication with an XML file.

## 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.

## Select a CD in the Box Below

Select a CD:
CD info will be listed here.

## AJAX Example Explained

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

    <html>
    <head>
    <script src="selectcd.js"></script>
    </head>

    <body>

    <form>
    Select a CD:
    <select name="cds" onchange="showCD(this.value)">
    <option value="Bob Dylan">Bob Dylan</option>
    <option value="Bonnie Tyler">Bonnie Tyler</option>
    <option value="Dolly Parton">Dolly Parton</option>
    </select>
    </form>

    <p>
    <div id="txtHint"><b>CD info will be listed here.</b></div>
    </p>

    </body>
    </html>

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 change the value in the drop down box, the function showCD is called.

The JavaScript code is listed below.

## The AJAX JavaScript

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

    var xmlHttp

    function showCD(str)
    {
    xmlHttp=GetXmlHttpObject();
    if (xmlHttp==null)
      {
      alert ("Your browser does not support AJAX!");
      return;
      }
    var url="getcd.asp";
    url=url+"?q="+str;
    url=url+"&sid="+Math.random();
    xmlHttp.onreadystatechange=stateChanged;
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);
    }

    function stateChanged()
    {
    if (xmlHttp.readyState==4)
    {
    document.getElementById("txtHint").innerHTML=xmlHttp.responseText;
    }
    }

    function GetXmlHttpObject()
    {
    var xmlHttp=null;
    try
      {
      // Firefox, Opera 8.0+, Safari
      xmlHttp=new XMLHttpRequest();
      }
    catch (e)
      {
      // Internet Explorer
      try
        {
        xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
        }
      catch (e)
        {
        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
      }
    return xmlHttp;
    }

## The XML File

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

## The AJAX Server Page

The server page called by the JavaScript, is a simple ASP file called "getcd.asp".

The page is written in VBScript for an Internet Information Server (IIS). It could easily be rewritten in PHP, or some other server language. Look at a corresponding example in PHP.

The code runs a query against an XML file and returns the result as HTML:

    <%
    response.expires=-1
    q=request.querystring("q")

    set xmlDoc=Server.CreateObject("Microsoft.XMLDOM")
    xmlDoc.async="false"
    xmlDoc.load(Server.MapPath("cd_catalog.xml"))

    set nodes=xmlDoc.selectNodes("CATALOG/CD[ARTIST='" & q & "']")

    for each x in nodes
      for each y in x.childnodes
        response.write("<b>" & y.nodename & ":</b> ")
        response.write(y.text)
        response.write("<br />")
      next
    next
    %>

From http://www.w3schools.com (Copyright Refsnes Data)