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