YouTip LogoYouTip

Php Ajax Xml

# PHP Example - AJAX and XML * * * AJAX can be used for interactive communication with XML files. * * * ## AJAX XML Example The following example will demonstrate how a web page can read information from an XML file using AJAX: ## Example CD info will be listed here... * * * ## Example Explained - HTML Page When a user selects a CD in the dropdown list above, a function called "showCD()" is executed. The function is triggered by the "onchange" event: function showCD(str){if (str==""){document.getElementById("txtHint").innerHTML="";return;} if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari browsersxmlhttp=new XMLHttpRequest();}else{// code for IE6, IE5 browsersxmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}xmlhttp.onreadystatechange=function(){if (xmlhttp.readyState==4 && xmlhttp.status==200){document.getElementById("txtHint").innerHTML=xmlhttp.responseText;}}xmlhttp.open("GET","getcd.php?q="+str,true);xmlhttp.send();} Select a CD: Select a CD:Bob DylanBonnie TylerDolly Parton
CD info will be listed here...
The showCD() function performs the following steps: * Check if a CD is selected * Create an XMLHttpRequest object * Create the function to be executed when the server response is ready * Send the request to a file on the server * Note that a parameter (q) is added to the end of the URL (containing the content of the dropdown list) * * * ## The PHP File The server page called by the JavaScript above is a PHP file named "getcd.php". The PHP script loads an XML document, "[cd_catalog.xml](#)", runs a query against the XML file, and returns the result as HTML: load("cd_catalog.xml"); $x=$xmlDoc->getElementsByTagName('ARTIST');for ($i=0; $ilength-1; $i++){// Process - only element node typeif ($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;$ilength;$i++){ // Process - only element node typeif ($cd->item($i)->nodeType==1){echo("" . $cd->item($i)->nodeName . ": ");echo($cd->item($i)->childNodes->item(0)->nodeValue);echo("
");}}?> When the CD query is sent from JavaScript to the PHP page, the following happens: 1. PHP creates an XML DOM object 2. It finds all elements to match the name passed from JavaScript 3. It outputs the album information and sends it back to the "txtHint" placeholder [](#)(#) (#)
← Php Ref TimezonesNg Ng Src β†’