AJAX XML - The JavaScript code

Your rating: None Average: 5 (2 votes)

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.
Anirban Roy's picture
Anirban Roy (not verified)
Re: AJAX XML - The JavaScript code

1)What does the method GetXmlHttpObject() do
2)why this is as this ' url=url+"?q="+str;
'Is it only used for cross page value pasing through url?

Post new comment

CAPTCHA
The question below is to prevent automated spam submissions.
2 + 17 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.