Ec (2); varxmlHttp & nbsp; functionshowUser (str) This is the function body called above {xmlHttpGetXmlHttpObject () first checks whether the browser supports HTTPRequestif (xmlHttpnull) script ec (2); script
Var xmlHttp function showUser (str) // This is the function body called above
{
XmlHttp = GetXmlHttpObject () // first, judge whether the browser supports HTTP Request.
If (xmlHttp = null)
{
Alert ("Browser does not support HTTP Request ")
Return
}
Var url = "getuser. php" // here is your PHP function name, which is the SQL statement written in this function.
Url = url + "? Q = "+ str // This is the following Parameter
Url = url + "& sid =" + Math. random () // he creates a random number here
XmlHttp. onreadystatechange = stateChanged // call stateChanged to determine the status (check some items here)
XmlHttp. open ("GET", url, true) // The URL passed in by OPEN is "asynchronous transmission", that is, the page is not refreshed.
XmlHttp. send (null) // send
}
Function stateChanged ()
{
If (xmlHttp. readyState = 4 | xmlHttp. readyState = "complete") // judge the status of readyState
{
Document. getElementById ("txtHint"). innerHTML = xmlHttp. responseText
// Are you familiar with the red part? The ID number in the preceding HTML page. Here, the innerHTML method of the returned result is written into txtHint.
}
} // The following function is a different creation method written for XMLHttpRequest without the browser's support.
Function GetXmlHttpObject ()
{
Var xmlHttp = null;
Try
{
// Firefox, Opera 8.0 +, Safari
XmlHttp = new XMLHttpRequest ();
}
Catch (e)
{
// Internet Explorer
Try
{
XmlHttp = new ActiveXObject ("Msxml2.XMLHTTP ");
}
Catch (e)
{
XmlHttp = new ActiveXObject ("Microsoft. XMLHTTP ");
}
}
Return xmlHttp;
}
$ 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 );
// The following echo statement is the HTML code to be returned to the front-end page. Finally, the following HTML code is displayed in
Inside the DIV of txtHint
Echo "";
While ($ row = mysql_fetch_array ($ result ))
{
Echo "[tr]";
Echo "[td]". $ row ['firstname']. "[/td]";
Echo "[td]". $ row ['lastname']. "[/td]";
Echo "[td]". $ row ['age']. "[/td]";
Echo "[td]". $ row ['hometown ']. "[/td]";
Echo "[td]". $ row ['job']. "[/td]";
Echo "[/tr]";
}
Echo "[table] [tr] [td] Firstname [/td] [td] Lastname [/td] [td] Age [/td] [td] Hometown [/td] [td] Job [/td] [/tr] [/table] ";
Mysql_close ($ con );
?>