Php+ajax Implement data invocation

Source: Internet
Author: User

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);
?>

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.