Examples of PHP and Ajax MySQL databases

Source: Internet
Author: User
Tags html form http request php file php and mysql database

Examples of PHP and Ajax MySQL databases

The user information is listed here.

This example contains four elements:

MySQL Database
A simple HTML form
A JavaScript
A PHP Web page



Database
We will use this database in this example to look like this:

Age
ID FirstName LastName Hometown Job
1 Peter Griffin 41 Quahog Brewery
2 Lois Griffin 40 Newport Piano Teacher
3 Joseph Swanson 39 Quahog Police Officer
4 Glenn Quagmire 41 Quahog Pilot

 
  
<form> 
Select a User:
<select name= "users" onchange= "Showuser (this.value)" >
<option Value= "1" >peter griffin</option>
<option value= "2" >lois griffin</option>-<option
Value= "3" >glenn quagmire</option>
<option value= "4" >joseph swanson</option>-</
Select>
</form>
<p>
<div id= "Txthint" ><b>user info would be listed here.</b></div>
</p>
</body>
   
     

For example the explanation-the H tml form
As you can see, this is just a simple HTML format for the dropdown box called "User" name and "id" from the database, as a choice of value.

The following form of the paragraph contains the so-called "Txthint" of a zone. The zone is a network server that is used as a location for reservation information retrieval.

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, function Showuser () is invoked.




The JavaScript
This is the JavaScript code stored in the file "Selectuser.js":

var xmlHttp
function Showuser (str)
{ 
xmlhttp=getxmlhttpobject ()
if (xmlhttp==null)
 {
 alert ("Browser does Not support HTTP Request "]
 return
 }
var url=" getuser.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")
 { 
 document.getElementById ("Txthint"). Innerhtml=xmlhttp.responsetext 
 } 
}
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;
}
     
       

For example
the statechanged () and Getxmlhttpobject functions are the same as PHP's AJAX chapters suggest that you can go there to explain these.

The Showuser () function

If an item is selected in the Drop-down box to perform the following function:

calls Getxmlhttpobject function to create a XMLHTTP object
defines the URL (file) to be transmitted to the server
Add a parameter (Q) to the URL with the content of the dropdown box
to add a random number to prevent the server from using the cache file
Call StateChanged When the change is triggered
opening XMLHTTP object with a specific URL.
Send an HTTP request to the server



PHP page
The server's page requirements for JavaScript, is a simple php file named "getuser.php".

The Web page is written in PHP and uses a MySQL database.

The code runs a database of SQL queries and returns the result as an HTML table:

 <?php $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 "<table border= ' 1 ' > <tr> <th>Firstname</th> <th>Lastname</th> <th>age

</th> <th>Hometown</th> <th>Job</th> </tr> ";
 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>"; Mysql_close ($con)?> reprint please indicate from Http://www.111cn.net/wy/yw.html 
Related Article

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.