Home > Backend Development > PHP Tutorial > About PHP examples - AJAX and XML interaction

About PHP examples - AJAX and XML interaction

jacklove
Release: 2023-03-25 14:38:02
Original
1404 people have browsed it

AJAX can be used to communicate interactively with XML files. This article will explain the interaction between AJAX and XML in detail.

AJAX XML Example

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

Example

Select a CD: Bob Dylan Bee Gees Cat Stevens

CD info will be listed here...

Example explanation - HTML page

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

<html><head><script>function showCD(str){    if (str=="")    {        document.getElementById("txtHint").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("txtHint").innerHTML=xmlhttp.responseText;        }    }    xmlhttp.open("GET","getcd.php?q="+str,true);    xmlhttp.send();}</script></head><body><form>Select a CD:<select name="cds" onchange="showCD(this.value)"><option value="">Select a CD:</option><option value="Bob Dylan">Bob Dylan</option><option value="Bonnie Tyler">Bonnie Tyler</option><option value="Dolly Parton">Dolly Parton</option></select></form><div id="txtHint"><b>CD info will be listed here...</b></div></body></html>
Copy after login

showCD() function will perform the following steps:

Check whether a CD is selected

to 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 (including the drop-down Contents of the list)

PHP file

The server page called by JavaScript in the above paragraph is a PHP file named "getcd.php".

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

<?php
$q=$_GET["q"];$xmlDoc = new DOMDocument();$xmlDoc->load("cd_catalog.xml");$x=$xmlDoc->getElementsByTagName(&#39;ARTIST&#39;);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>");    }}?>
Copy after login

When the CD query is sent from JavaScript When reaching the PHP page, what will happen:

PHP creates the XML DOM object

Find all names in the element that match the data passed by JavaScript

Output album information and send back the "txtHint" placeholder

This article explains the interaction between AJAX and XML in detail. For more learning materials, please pay attention to the php Chinese website.

Related recommendations:

About PHP - The interaction between AJAX and MySQL

About PHP - The interaction between AJAX and PHP The connection between

Related knowledge about PHP Simple XML

The above is the detailed content of About PHP examples - AJAX and XML interaction. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template