AJAX XML: The PHP File


Course information will be listed here.
The page on the server called by the JavaScript is a PHP file called GetCourse.php as below, which loads an XML document, CourseList.xml, runs a query against the XML file, and returns the result as HTML. When the course query is sent from the JavaScript to the PHP page, the following happens:
  1. PHP creates an XML DOM object.

  2. Find the number element that matches the course number sent from the JavaScript.

  3. Output the course information (send to the txtHint placeholder)
<?php
  $q = $_GET["q"]; 
  $xmlDoc = new DOMDocument( );
  $xmlDoc->load( "CourseList.xml" );
  $x = $xmlDoc->getElementsByTagName( 'number' );
  for ( $i=0; $i<=$x->length-1; $i++ ) {
    // Process only element nodes.
    if ( $x->item($i)->nodeType == 1 ) {  // 1: element
      if ( $x->item($i)->childNodes->item(0)->nodeValue == $q ) {
        $y = ( $x->item($i)->parentNode );
      }
    }
  }
  $course = ( $y->childNodes );
  for ( $i=0; $i<$course->length; $i++ ) {
    // Process only element nodes.
    if ( $course->item($i)->nodeType == 1 ) {
      echo( $course->item($i)->nodeName . ":" );
      echo( $course->item($i)->childNodes->item(0)->nodeValue );
    }
  }
?>