Go PHP and AJAX MySQL Database Example

Source: Internet
Author: User
Reference Address: http://www.w3schools.com/PHP/php_ajax_database.asp

AJAX can is used for interactive communication with a database.

AJAX Database Example

In the Ajax example below we'll demonstrate how a Web page can fetch information from a MySQL database using AJAX Techno Logy.

Select a Name in the Box Below

Select a user:peter Griffin Lois Griffin Joseph Swanson Glenn Quagmire

User info'll be listed here.

This example consists of four elements:

A MySQL database A simple HTML form a JavaScript a PHP page

The Database

The database we'll be a using in this example looks like this:

ID FirstName LastName Age 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

The HTML Form

The example above contains a simple HTML form and a link to a JavaScript:

                                                

User info'll be listed here.

            

Example explained-the HTML Form

As you can see it's just a simple HTML form with a drop-down box called "users" with names and the "id" from the database As option values.

The paragraph below the form contains a div called "txthint". The div is used as a placeholder for info retrieved from the Web server.

When the user selects data, a function called "Showuser ()" is executed. The execution of the function is triggered by the "onchange" event.

In other Words:each time the user changes the value of the drop box, the function Showuser () is called.

The JavaScript

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

var xmlHttp
function Showuser (str)            {            xmlhttp=getxmlhttpobject ()            if (xmlhttp==null)            {            alert ("Browser does Not the 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;            }

Example Explained

The statechanged () and Getxmlhttpobject functions is the same as in the PHP AJAX Suggest chapter, you can go to there for An explanation of those.

The Showuser () Function

If an item in the Drop box is selected the function executes the following:

Calls on the Getxmlhttpobject function to create an XMLHTTP object defines the URL (filename) to send to the server Adds a Parameter (q) to the URL with the content of the dropdown box Adds a random number to prevent the server from using a CAC Hed file Call StateChanged When a change was triggered Opens the XMLHTTP object with the given URL. Sends an HTTP request to the server

The PHP Page

The server page called by the JavaScript, was a simple PHP file called "getuser.php".

The page is written in PHP and uses a MySQL databse.

The code runs a SQL query against a database and returns the result as an HTML table:

Age "; while ($row = Mysql_fetch_array ($result)) {echo " "; echo " "; echo " "; echo " "; echo " "; echo " "; echo " "; } echo "
     
                 
Firstname Lastname Hometown Job
" . $row [' FirstName ']. "" . $row [' LastName ']. "" . $row [' Age ']. "" . $row [' Hometown ']. "" . $row [' Job ']. "
"; Mysql_close ($con);?>

Example Explained

When the. sent from the JavaScript to the PHP page the following happens:

PHP opens a connection to a MySQL server the ' user ' with the specified name was found a table is created and the data are in Serted and sent to the "txthint" placeholder
  • 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.