Var xmlhttp function showuser (str)// This is the function body
{
Xmlhttp=getxmlhttpobject () called above, which first determines 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 pull.
URL =url+ "q=" +str //This is the following argument
url=url+ "&sid=" +math.random () //He created a random number here
xmlhttp.onreadystatechange=statechanged //Call statechanged judgment State (here's some things to check out)
Xmlhttp.open ("Get", Url,true) //open The URL true to pass in is to represent an asynchronous transfer, that is, the page does not refresh
Xmlhttp.send (NULL)/Send
}
function statechanged ()
{
if (xmlhttp.readystate==4 | | xmlhttp.readystate== "complete")//Judge the status of ReadyState
{
document.getElementById ("Txthint"). Innerhtml=xmlhttp.responsetext
Is the red part familiar? is the ID number in the HTML page above. Here is the method that returns the result innerHTML into Txthint
}
The following function is a different creation method written for the different support of browsers for XMLHttpRequest
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 return to the foreground page. And finally, the following HTML code appears in the ID
Txthint's Div inside
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);
?>