AJAX XML - The JavaScript code

Your rating: None Average: 5 (1 vote)

AJAX is used for interactive communication with an XML file. Website may provide information from an XML file using AJAX search technology, if you select an item in a list box.

Select a CD:

TITLE: One night only
ARTIST: Bee Gees
COUNTRY: UK
COMPANY: Polydor
PRICE: 10.90
YEAR: 1998

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.php";
  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()
 {
  if (window.XMLHttpRequest)
   {
   // code for IE7+, Firefox, Chrome, Opera, Safari
    return new XMLHttpRequest();
   }
  if (window.ActiveXObject)
    {
    // code for IE6, IE5
    return new ActiveXObject("Microsoft.XMLHTTP");
    }
    return null;
 }

Information taken from the PHP Tutorials

Please try to help out with unanswered topics on the forum. Chances are you have had the same issue/question some time in your IT career!

Comments

Have a question? Please ask it on the forum instead.

Post new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
The question below is to prevent automated spam submissions.
3 + 13 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.