PHP development basic tutorial AJAX and XML

AJAX and XML examples:

AJAX can communicate interactively with XML files

The following example will demonstrate how a web page reads information from an XML file through AJAX:

This example consists of three parts

  • HTML form page

  • PHP page

  • XML file


##HTML form Page

When the user selects a CD in the drop-down list above, a function named "showCD()" will be executed. This function is triggered by the "onchange" event:

See 1.php

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script>
function showCD(str){
	if(str==""){
		document.getElementById("txt").innerHTML="";
		return;
	}
	if(window.XMLHttpRequest){
		// IE7+, Firefox, Chrome, Opera, Safari 浏览器执行
		xmlhttp=new XMLHttpRequest();
	}else{
		//IE6,IE5浏览器执行
		xmlhttp =new ActiveXObject("MIcrosoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange=function(){
		if(xmlhttp.readyState==4&&xmlhttp.status==200){
			document.getElementById("txt").innerHTML=xmlhttp.responseText;
		}
	}
	xmlhttp.open("GET","2.php?q="+str,true);
	xmlhttp.send();
}
</script>
</head>
<body>
<form>
选择一个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">Bonnie Tyler</option>
</select>
</form>
</br>
<div id="txt"><b>选择下拉列表,显示详细信息</b></div>
</body>
</html>

for the source code after the user selects the drop-down list. Call the showCD() function

ShowCD() function to perform the following steps:

  • Check whether a CD is selected

  • Create XMLHttpRequest object

  • Creates a function that is executed when the server response is ready

  • Sends a request to a file on the server

  • Please note the parameter (q) added to the end of the URL (containing the contents of the drop-down list)


PHP file

The server page called above through JavaScript is a PHP file named "2.php".

The PHP script loads the XML document, "3.xml", runs the query against the XML file, and returns the results in HTML:

See 2.php for the source code

<?php
$q=$_GET["q"];
$xmlDoc = new DOMDocument();
$xmlDoc->load("3.xml");
$x=$xmlDoc->getElementsByTagName('ARTIST');
for ($i=0; $i<=$x->length-1; $i++)
{
	// 处理元素节点
	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++)
{ 
	// 处理元素节点
	if ($cd->item($i)->nodeType==1)
	{
		echo("<b>" . $cd->item($i)->nodeName . ":</b> ");
		echo($cd->item($i)->childNodes->item(0)->nodeValue);
		echo("<br>");
	}
}
?>

When the CD query is sent from JavaScript to the PHP page, what happens:

  • PHP creates the XML DOM object of the "8_3.xml" file

  • Loop through all "artist" elements (nodetypes = 1) and look for names that match the data passed by JavaScript

  • Find the correct artist included in the CD

  • Output the album information and send it to "txtHint" placeholder


##XML file See 3.xml for source code

<?xml version="1.0" encoding="ISO-8859-1"?>

<CATALOG>
	<CD>
		<TITLE>Empire Burlesque</TITLE>
		<ARTIST>Bob Dylan</ARTIST>
		<COUNTRY>USA</COUNTRY>
		<COMPANY>Columbia</COMPANY>
		<PRICE>10.90</PRICE>
		<YEAR>1985</YEAR>
	</CD>
	<CD>
		<TITLE>Hide your heart</TITLE>
		<ARTIST>Bonnie Tyler</ARTIST>
		<COUNTRY>UK</COUNTRY>
		<COMPANY>CBS Records</COMPANY>
		<PRICE>9.90</PRICE>
		<YEAR>1988</YEAR>
	</CD>
	<CD>
		<TITLE>Greatest Hits</TITLE>
		<ARTIST>Dolly Parton</ARTIST>
		<COUNTRY>USA</COUNTRY>
		<COMPANY>RCA</COMPANY>
		<PRICE>9.90</PRICE>
		<YEAR>1982</YEAR>
	</CD>
	<CD>
		<TITLE>Still got the blues</TITLE>
		<ARTIST>Gary Moore</ARTIST>
		<COUNTRY>UK</COUNTRY>
		<COMPANY>Virgin records</COMPANY>
		<PRICE>10.20</PRICE>
		<YEAR>1990</YEAR>
	</CD>
	<CD>
		<TITLE>Eros</TITLE>
		<ARTIST>Eros Ramazzotti</ARTIST>
		<COUNTRY>EU</COUNTRY>
		<COMPANY>BMG</COMPANY>
		<PRICE>9.90</PRICE>
		<YEAR>1997</YEAR>
	</CD>
	<CD>
		<TITLE>One night only</TITLE>
		<ARTIST>Bee Gees</ARTIST>
		<COUNTRY>UK</COUNTRY>
		<COMPANY>Polydor</COMPANY>
		<PRICE>10.90</PRICE>
		<YEAR>1998</YEAR>
	</CD>
	<CD>
		<TITLE>Sylvias Mother</TITLE>
		<ARTIST>Dr.Hook</ARTIST>
		<COUNTRY>UK</COUNTRY>
		<COMPANY>CBS</COMPANY>
		<PRICE>8.10</PRICE>
		<YEAR>1973</YEAR>
	</CD>
	<CD>
		<TITLE>Maggie May</TITLE>
		<ARTIST>Rod Stewart</ARTIST>
		<COUNTRY>UK</COUNTRY>
		<COMPANY>Pickwick</COMPANY>
		<PRICE>8.50</PRICE>
		<YEAR>1990</YEAR>
	</CD>
	<CD>
		<TITLE>Romanza</TITLE>
		<ARTIST>Andrea Bocelli</ARTIST>
		<COUNTRY>EU</COUNTRY>
		<COMPANY>Polydor</COMPANY>
		<PRICE>10.80</PRICE>
		<YEAR>1996</YEAR>
	</CD>
	<CD>
		<TITLE>When a man loves a woman</TITLE>
		<ARTIST>Percy Sledge</ARTIST>
		<COUNTRY>USA</COUNTRY>
		<COMPANY>Atlantic</COMPANY>
		<PRICE>8.70</PRICE>
		<YEAR>1987</YEAR>
	</CD>
	<CD>
		<TITLE>Black angel</TITLE>
		<ARTIST>Savage Rose</ARTIST>
		<COUNTRY>EU</COUNTRY>
		<COMPANY>Mega</COMPANY>
		<PRICE>10.90</PRICE>
		<YEAR>1995</YEAR>
	</CD>
	<CD>
		<TITLE>1999 Grammy Nominees</TITLE>
		<ARTIST>Many</ARTIST>
		<COUNTRY>USA</COUNTRY>
		<COMPANY>Grammy</COMPANY>
		<PRICE>10.20</PRICE>
		<YEAR>1999</YEAR>
	</CD>
	<CD>
		<TITLE>For the good times</TITLE>
		<ARTIST>Kenny Rogers</ARTIST>
		<COUNTRY>UK</COUNTRY>
		<COMPANY>Mucik Master</COMPANY>
		<PRICE>8.70</PRICE>
		<YEAR>1995</YEAR>
	</CD>
	<CD>
		<TITLE>Big Willie style</TITLE>
		<ARTIST>Will Smith</ARTIST>
		<COUNTRY>USA</COUNTRY>
		<COMPANY>Columbia</COMPANY>
		<PRICE>9.90</PRICE>
		<YEAR>1997</YEAR>
	</CD>
	<CD>
		<TITLE>Tupelo Honey</TITLE>
		<ARTIST>Van Morrison</ARTIST>
		<COUNTRY>UK</COUNTRY>
		<COMPANY>Polydor</COMPANY>
		<PRICE>8.20</PRICE>
		<YEAR>1971</YEAR>
	</CD>
	<CD>
		<TITLE>Soulsville</TITLE>
		<ARTIST>Jorn Hoel</ARTIST>
		<COUNTRY>Norway</COUNTRY>
		<COMPANY>WEA</COMPANY>
		<PRICE>7.90</PRICE>
		<YEAR>1996</YEAR>
	</CD>
	<CD>
		<TITLE>The very best of</TITLE>
		<ARTIST>Cat Stevens</ARTIST>
		<COUNTRY>UK</COUNTRY>
		<COMPANY>Island</COMPANY>
		<PRICE>8.90</PRICE>
		<YEAR>1990</YEAR>
	</CD>
	<CD>
		<TITLE>Stop</TITLE>
		<ARTIST>Sam Brown</ARTIST>
		<COUNTRY>UK</COUNTRY>
		<COMPANY>A and M</COMPANY>
		<PRICE>8.90</PRICE>
		<YEAR>1988</YEAR>
	</CD>
	<CD>
		<TITLE>Bridge of Spies</TITLE>
		<ARTIST>T'Pau</ARTIST>
		<COUNTRY>UK</COUNTRY>
		<COMPANY>Siren</COMPANY>
		<PRICE>7.90</PRICE>
		<YEAR>1987</YEAR>
	</CD>
	<CD>
		<TITLE>Private Dancer</TITLE>
		<ARTIST>Tina Turner</ARTIST>
		<COUNTRY>UK</COUNTRY>
		<COMPANY>Capitol</COMPANY>
		<PRICE>8.90</PRICE>
		<YEAR>1983</YEAR>
	</CD>
	<CD>
		<TITLE>Midt om natten</TITLE>
		<ARTIST>Kim Larsen</ARTIST>
		<COUNTRY>EU</COUNTRY>
		<COMPANY>Medley</COMPANY>
		<PRICE>7.80</PRICE>
		<YEAR>1983</YEAR>
	</CD>
	<CD>
		<TITLE>Pavarotti Gala Concert</TITLE>
		<ARTIST>Luciano Pavarotti</ARTIST>
		<COUNTRY>UK</COUNTRY>
		<COMPANY>DECCA</COMPANY>
		<PRICE>9.90</PRICE>
		<YEAR>1991</YEAR>
	</CD>
	<CD>
		<TITLE>The dock of the bay</TITLE>
		<ARTIST>Otis Redding</ARTIST>
		<COUNTRY>USA</COUNTRY>
		<COMPANY>Atlantic</COMPANY>
		<PRICE>7.90</PRICE>
		<YEAR>1987</YEAR>
	</CD>
	<CD>
		<TITLE>Picture book</TITLE>
		<ARTIST>Simply Red</ARTIST>
		<COUNTRY>EU</COUNTRY>
		<COMPANY>Elektra</COMPANY>
		<PRICE>7.20</PRICE>
		<YEAR>1985</YEAR>
	</CD>
	<CD>
		<TITLE>Red</TITLE>
		<ARTIST>The Communards</ARTIST>
		<COUNTRY>UK</COUNTRY>
		<COMPANY>London</COMPANY>
		<PRICE>7.80</PRICE>
		<YEAR>1987</YEAR>
	</CD>
	<CD>
		<TITLE>Unchain my heart</TITLE>
		<ARTIST>Joe Cocker</ARTIST>
		<COUNTRY>USA</COUNTRY>
		<COMPANY>EMI</COMPANY>
		<PRICE>8.20</PRICE>
		<YEAR>1987</YEAR>
	</CD>
</CATALOG>

This file contains data about CD collection


Learning experience This example mainly includes the following knowledge points:

    Form basics: drop-down options
  • onchange event: content in the field Changes occur
  • Function calls, function value transfer
  • Creation of AJAX XMLHttpRequest objects, functions executed when the server responds, and transfer to the server File sending request on: See 1-5 for learning experience
  • HTML DOM getElementById() method: Returns a reference to the first object with the specified ID
  • XML related knowledge

    Create XML DOM object
  • Load XML file to new XML DOM object
  • Get the object of a specific tag name: getElementsByTagName()

  • Get the child section collection of a specific element: HTML DOM childNodes

  • Get the node value of the first button element: HTML DOM nodeValue

  • Get the node type of the body element: HTML DOM nodeType

Continuing Learning
||
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script> function showCD(str){ if(str==""){ document.getElementById("txt").innerHTML=""; return; } if(window.XMLHttpRequest){ // IE7+, Firefox, Chrome, Opera, Safari 浏览器执行 xmlhttp=new XMLHttpRequest(); }else{ //IE6,IE5浏览器执行 xmlhttp =new ActiveXObject("MIcrosoft.XMLHTTP"); } xmlhttp.onreadystatechange=function(){ if(xmlhttp.readyState==4&&xmlhttp.status==200){ document.getElementById("txt").innerHTML=xmlhttp.responseText; } } xmlhttp.open("GET","2.php?q="+str,true); xmlhttp.send(); } </script> </head> <body> <form> 选择一个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">Bonnie Tyler</option> </select> </form> </br> <div id="txt"><b>选择下拉列表,显示详细信息</b></div> </body> </html>
submitReset Code