Php + ajax data call-PHP source code

Source: Internet
Author: User
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 );
?>

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.