You are on page 1of 3

1/28/2014

PHP Example AJAX and MySQL

HOME

HTML

CSS

JAVASCRIPT

JQUERY

XML

ASP.NET

PHP

SQL

MORE...

REFERENCES

EXAMPLES

FORUM

PHP Basic
PHP HOME PHP Intro PHP Install PHP Syntax PHP Variables PHP Echo / Print PHP Data Types PHP String Functions PHP Constants PHP Operators PHP If...Else...Elseif PHP Switch PHP While Loops PHP For Loops PHP Functions PHP Arrays PHP Sorting Arrays PHP Superglobals

Search w3schools.com

Select Language

WEB HOSTING

PHP - AJAX and MySQL


Previous
AJAX can be used for interactive communication with a database.

Best Web Hostin Developers Love

UK Reseller Host

Next Chapter

WEB BUILDING

XML Editor - Free T

AJAX Database Example


The following example will demonstrate how a web page can fetch information from a database with AJAX:

FREE Website BUIL

FREE Website C re

W3SCHOOLS EXA

Example
Lois Griffin
Firstname Lastname Age Hometown Job

HTML5, C SS, JavaS PHP, jQuery, XML, ASP C ertification

SHARE THIS PA

PHP Forms
PHP Form Handling PHP Form Validation PHP Form Required PHP Form URL/E-mail PHP Form Complete

Lois

Griffin

40

Newport

Piano Teacher

Example Explained - The MySQL Database


The database table we use in the example above looks like this:
id FirstName LastName Age Hometown Job

PHP Advanced
PHP Arrays Multi PHP Date PHP Include PHP File PHP File Upload PHP Cookies PHP Sessions PHP E-mail PHP Secure E-mail PHP Error PHP Exception PHP Filter

1 2 3 4

Peter Lois Joseph Glenn

Griffin Griffin Swanson Quagmire

41 40 39 41

Quahog Newport Quahog Quahog

Brewery Piano Teacher Police Officer Pilot

Example Explained - The HTML Page


When a user selects a user in the dropdown list above, a function called "showUser()" is executed. The function is triggered by the "onchange" event:

PHP Database
PHP MySQL Intro PHP MySQL Connect PHP CREATE DB/Table PHP INSERT INTO PHP SELECT PHP WHERE PHP ORDER BY PHP UPDATE PHP DELETE PHP ODBC

PHP XML
XML Expat Parser XML DOM XML SimpleXML

PHP and AJAX


AJAX Intro AJAX PHP AJAX Database AJAX XML AJAX Live Search AJAX RSS Reader AJAX Poll

PHP Examples

< h t m l > < h e a d > < s c r i p t > f u n c t i o ns h o w U s e r ( s t r ) { i f( s t r = = " " ) { d o c u m e n t . g e t E l e m e n t B y I d ( " t x t H i n t " ) . i n n e r H T M L = " " ; r e t u r n ; } i f( w i n d o w . X M L H t t p R e q u e s t ) { / /c o d ef o rI E 7 + ,F i r e f o x ,C h r o m e ,O p e r a ,S a f a r i x m l h t t p = n e wX M L H t t p R e q u e s t ( ) ; } e l s e { / /c o d ef o rI E 6 ,I E 5 x m l h t t p = n e wA c t i v e X O b j e c t ( " M i c r o s o f t . X M L H T T P " ) ; } x m l h t t p . o n r e a d y s t a t e c h a n g e = f u n c t i o n ( ) { i f( x m l h t t p . r e a d y S t a t e = = 4& &x m l h t t p . s t a t u s = = 2 0 0 ) { d o c u m e n t . g e t E l e m e n t B y I d ( " t x t H i n t " ) . i n n e r H T M L = x m l h t t p . r e s p o n s e T e x t ; } } x m l h t t p . o p e n ( " G E T " , " g e t u s e r . p h p ? q = " + s t r , t r u e ) ; x m l h t t p . s e n d ( ) ; } < / s c r i p t > < / h e a d > < b o d y >

WEB RESOURC

Web Statistics

Web Validation

http://www.w3schools.com/php/php_ajax_database.asp

1/3

1/28/2014
PHP Examples PHP Quiz PHP Certificate

PHP Example AJAX and MySQL


< f o r m > < s e l e c tn a m e = " u s e r s "o n c h a n g e = " s h o w U s e r ( t h i s . v a l u e ) " > < o p t i o nv a l u e = " " > S e l e c tap e r s o n : < / o p t i o n > < o p t i o nv a l u e = " 1 " > P e t e rG r i f f i n < / o p t i o n > < o p t i o nv a l u e = " 2 " > L o i sG r i f f i n < / o p t i o n > < o p t i o nv a l u e = " 3 " > G l e n nQ u a g m i r e < / o p t i o n > < o p t i o nv a l u e = " 4 " > J o s e p hS w a n s o n < / o p t i o n > < / s e l e c t > < / f o r m > < b r > < d i vi d = " t x t H i n t " > < b > P e r s o ni n f ow i l lb el i s t e dh e r e . < / b > < / d i v > < / b o d y > < / h t m l >
The showUser() function does the following: Check if a person is selected Create an XMLHttpRequest object Create the function to be executed when the server response is ready Send the request off to a file on the server Notice that a parameter (q) is added to the URL (with the content of the dropdown list)

PHP Reference
PHP Array PHP Calendar PHP Date PHP Directory PHP Error PHP Filesystem PHP Filter PHP FTP PHP HTTP PHP Libxml PHP Mail PHP Math PHP Misc PHP MySQLi PHP SimpleXML PHP String PHP XML PHP Zip PHP Timezones

The PHP File


The page on the server called by the JavaScript above is a PHP file called "getuser.php". The source code in "getuser.php" runs a query against a MySQL database, and returns the result in an HTML table:

< ? p h p $ q=i n t v a l ( $ _ G E T [ ' q ' ] ) ; $ c o n=m y s q l i _ c o n n e c t ( ' l o c a l h o s t ' , ' p e t e r ' , ' a b c 1 2 3 ' , ' m y _ d b ' ) ; i f( ! $ c o n ) { d i e ( ' C o u l dn o tc o n n e c t :'.m y s q l i _ e r r o r ( $ c o n ) ) ; } m y s q l i _ s e l e c t _ d b ( $ c o n , " a j a x _ d e m o " ) ; $ s q l = " S E L E C T*F R O Mu s e rW H E R Ei d=' " . $ q . " ' " ; $ r e s u l t=m y s q l i _ q u e r y ( $ c o n , $ s q l ) ; e c h o" < t a b l eb o r d e r = ' 1 ' > < t r > < t h > F i r s t n a m e < / t h > < t h > L a s t n a m e < / t h > < t h > A g e < / t h > < t h > H o m e t o w n < / t h > < t h > J o b < / t h > < / t r > " ; w h i l e ( $ r o w=m y s q l i _ f e t c h _ a r r a y ( $ r e s u l t ) ) { e c h o" < t r > " ; e c h o" < t d > ".$ r o w [ ' F i r s t N a m e ' ]." < / t d > " ; e c h o" < t d > ".$ r o w [ ' L a s t N a m e ' ]." < / t d > " ; e c h o" < t d > ".$ r o w [ ' A g e ' ]." < / t d > " ; e c h o" < t d > ".$ r o w [ ' H o m e t o w n ' ]." < / t d > " ; e c h o" < t d > ".$ r o w [ ' J o b ' ]." < / t d > " ; e c h o" < / t r > " ; } e c h o" < / t a b l e > " ; m y s q l i _ c l o s e ( $ c o n ) ; ? >
Explanation: When the query is sent from the JavaScript to the PHP file, the following happens: 1. PHP opens a connection to a MySQL server 2. The correct person is found 3. An HTML table is created, filled with data, and sent back to the "txtHint" placeholder

Previous

Next Chapter

http://www.w3schools.com/php/php_ajax_database.asp

2/3

1/28/2014

PHP Example AJAX and MySQL

Top 10 Tutorials
HTML Tutorial HTML5 Tutorial C SS Tutorial C SS3 Tutorial JavaScript Tutorial jQuery Tutorial SQL Tutorial PHP Tutorial ASP.NET Tutorial XML Tutorial

Top 10 References
HTML/HTML5 Reference C SS 1,2,3 Reference C SS 3 Browser Support JavaScript HTML DOM XML DOM PHP Reference jQuery Reference ASP.NET Reference HTML C olors

Top 10 Examples
HTML Examples C SS Examples JavaScript Examples HTML DOM Examples PHP Examples jQuery Examples XML Examples XML DOM Examples ASP Examples SVG Examples

Web Certificates
HTML C ertificate HTML5 C ertificate C SS C ertificate JavaScript C ertificate jQuery C ertificate XML C ertificate ASP C ertificate PHP C ertificate

Color Picker

R EPO R T ER R O R

HO ME |

TO P

PR INT

FO R UM

ABO UT

ADVER TISE W ITH US

W3Schools is optimized for learning, testing, and training. Examples might be simplified to improve reading and basic understanding. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using this site, you agree to have read and accepted our terms of use, cookie and privacy policy. C opyright 1999-2014 by Refsnes Data. All Rights Reserved.

http://www.w3schools.com/php/php_ajax_database.asp

3/3

You might also like