PHP and AJAX Responsexml Instance tutorial

Source: Internet
Author: User
Tags html form php and

PHP and AJAX Responsexml Instance tutorial

Ajax technology can be used to recover the database information for XML.




Ajax database for example of XML
In the following example of Ajax, we will demonstrate how Web pages can be sold to information from a MySQL database, which converts it to an XML document and uses it to display information in several different places.

This example seems to me like "PHP Ajax Technology Database" for example, in the last chapter, but there is a big difference: In this example we get the data from the PHP XML page using the Responsexml function.

The received response as an XML file allows us to update several places on this page, rather than just accept a PHP output and display it.

In this example, we will update some <span> content with the information we receive from the database.

Age
ID FirstName LastName Hometown Job
1 Peter Griffin 41 Quahog Brewery
2 Lois Griffin 40 Newport Piano Teacher
3 Joseph Swanson 39 Quahog Police Officer
4 Glenn Quagmire 41 Quahog Pilot

 
 
<form> 
Select a User:
<select name= "users" onchange= "Showuser (this.value)" >
<option Value= "1" >peter griffin</option>
<option value= "2" >lois griffin</option>-<option
Value= "3" >glenn quagmire</option>
<option value= "4" >joseph swanson</option>-</
Select>
</form>
 
 
</body>
  
     

For example the explanation-the H tml form
The HTML form is a drop-down box called "User" name and "id" from the database, as the value of the selection.
The following form has several different <span> elements that are used as placeholders for different values that we will retrieve.
When the user selects the data, a function called "showuser ()" executes. The function performed is the "onchange" event that is raised.
In other words: each change in the value of the user in the dropdown box, the function Showuser () is invoked and the output results in the specified <span> content.


Reprint please indicate from http://www.111cn.net/wy/yw.html


The JavaScript
This is the JavaScript code stored in the file "Responsexml.js":

var xmlHttp
function Showuser (str)
 { 
 xmlhttp=getxmlhttpobject ()
 if (xmlhttp==null)
  {
  alert ("Browser does Not support HTTP Request "]
  return
  } 
 var url=" responsexml.php "
 url=url+" q= "+str url=url+"
 & Sid= "+math.random ()
 xmlhttp.onreadystatechange=statechanged 
 xmlhttp.open (" Get ", Url,true)
 Xmlhttp.send (null)
 }
 function statechanged () {if (xmlhttp.readystate==4 | | xmlhttp.readystate== "complete") {Xmldoc=xmlhttp.responsex
 ML; document.getElementById ("FirstName"). Innerhtml= xmldoc.getelementsbytagname ("FirstName") [0].childnodes[0].
 NodeValue; document.getElementById ("LastName"). Innerhtml= xmldoc.getelementsbytagname ("LastName") [0].childnodes[0].
 NodeValue;
 document.getElementById ("Job"). Innerhtml= xmldoc.getelementsbytagname ("job") [0].childnodes[0].nodevalue;
 document.getElementById ("Age_text"). Innerhtml= "Age:";
 document.getElementById (' age '). Innerhtml= xmldoc.getelementsbytagname ("Age") [0].childnodes[0].nodevalue;
 document.getElementById ("Hometown_text"). Innerhtml= "<br/>from:"; document.getElementById ("Hometown"). Innerhtml= xmldoc.getelementsbytagname ("Hometown") [0].childNodes[0].
 NodeValue; }
}
function Getxmlhttpobject ()
 { 
 var objxmlhttp=null
 if (window). XMLHttpRequest)
  {
  objxmlhttp=new XMLHttpRequest ()
  }
 else if (window. ActiveXObject)
  {
  objxmlhttp=new activexobject ("Microsoft.XMLHTTP")
  } return
 objxmlhttp
 }
    
       

For example to explain
The Showuser () and Getxmlhttpobject functions are the same PHP Ajax chapter database that you can go there to explain these.

The statechanged () function

If an item chooses to perform in a Drop-down box, the following functions are available:

Defines a "syntax" variable that uses the Responsexml function as an XML file
Retrieving data from XML files and making them in the right <span> content



PHP page
The server's page requirements for JavaScript, is a simple php file named "responsexml.php".

The page is written in PHP and uses a MySQL database.

The code runs a database of SQL queries and returns the result as an XML file:

<?php
Header (' Content-type:text/xml ');
Header ("Cache-control:no-cache, must-revalidate");
A Date
in the past header ("Expires:mon, 1997 05:00:00 GMT");
$q =$_get["Q"];
$con = mysql_connect (' localhost ', ' Peter ', ' abc123 ');
if (! $con)
 {
 die (' Could not connect: '. Mysql_error ());
 }
mysql_select_db ("Ajax_demo", $con);
$sql = "SELECT * FROM user WHERE id =". $q. "";
$result = mysql_query ($sql);
Echo ' <?xml version= ' 1.0 ' encoding= ' iso-8859-1 '?>
<person> ';
while ($row = Mysql_fetch_array ($result))
 {
 echo <firstname>. $row [' FirstName ']. "</firstname>";
 echo "<lastname>". $row [' LastName ']. "</lastname>";
 echo "<age>". $row [' Age ']. "</age>";
 echo "
Mysql_close ($con);
? >
Reprint please indicate from http://www.111cn.net/wy/yw.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.