PHP and Ajax MySQL Database

Source: Internet
Author: User
Tags html form http request

Examples of PHP and Ajax MySQL databases

Examples of AJAX databases
In the following example of Ajax, we will demonstrate how Web pages can be sold to information from a MySQL database using AJAX technology.

Examples of AJAX databases
In an HTML form
The example above contains a simple HTML table and links to a javascript:

 
   
<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 to explain
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 chooses to perform in a Drop-down box, the following functions are available:

Call the 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 contents of the dropdown box
Add a random number to prevent the server from using cache files
When call statechanged changes, it triggers
Opening XMLHTTP objects with specific URLs.
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 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);
? >
All right, our php+ajax is done.

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.