AJAX XML - The PHP Page code

Your rating: None Average: 3.3 (4 votes)

AJAX can be used for interactive communication with an XML file. A web page can fetch information from an XML file with AJAX technology, e.g when you select an item from a dropdown box.

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

The PHP script loads an XML document, "cd_catalog.xml", runs a query against the XML file, and returns the result as HTML:


<?php
 $q=$_GET["q"];
 $xmlDoc = new DOMDocument();
 $xmlDoc->load("cd_catalog.xml");
 $x=$xmlDoc->getElementsByTagName('ARTIST');
 for ($i=0; $i<=$x->length-1; $i++)
    {
//Process only element nodes
   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++)
   {
//Process only element nodes
  if ($cd->item($i)->nodeType==1)
   {
  echo($cd->item($i)->nodeName);
  echo(": ");
  echo($cd->item($i)->childNodes->item(0)->nodeValue);
  echo("<br />");
    }
 }
 ?>

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.
TT's picture
TT (not verified)
Re: AJAX XML - The PHP Page code

Good post.
Can you explain how this can be bundled in P/s environment ?

How will the HTML definition look like ? Where will the php script be stored ? Will any changes be required to WEBXML file etc ? Any specialy config needed to handle security to run the php script?