(转)PHP and AJAX MySQL Database Example
AJAX can be used for interactive communication with a database.
AJAX Database Example
In the AJAX example below we will demonstrate how a web page can fetch information from a MySQL database using AJAX technology.
Select a Name in the Box Below
Select a User: Peter Griffin Lois Griffin Joseph Swanson Glenn Quagmire
User info will be listed here.
This example consists of four elements:
a MySQL database a simple HTML form a JavaScript a PHP pageThe Database
The database we will be using in this example looks like this:
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:
|
As you can see it is 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 in the drop down box, the function showUser() is called.
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; } |
The stateChanged() and GetXmlHttpObject functions are 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 down 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 cached file Call stateChanged when a change is triggered Opens the XMLHTTP object with the given url. Sends an HTTP request to the serverThe PHP Page
The server page called by the JavaScript, is 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:
|
When the query is 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 is found A table is created and the data is inserted and sent to the "txtHint" placeholder推荐阅读
-
(转)PHP and AJAX MySQL Database Example
-
Windows2000 + Apache + MySql + PHP3 + PHP4 + PERL安装使用小结(转)_PHP
-
ThinkPHP结合ajax、Mysql实现的客户端通信功能代码示例_PHP
-
PHP+Mysql+Ajax实现淘宝客服聊天功能
-
CentOS+Nginx+PHP+Mysql(3)(转)
-
解决了Ajax、MySQL 和 Zend Framework 的乱码问题_PHP教程
-
MySQL数据库格式轻松转_PHP教程
-
PHP+jQuery+Ajax+Mysql实现发表心情功能
-
php+mysql 采用ajax技术的 省 市 地 3级联动无刷新菜单 源码
-
如何使用PHP+Mysql+Ajax来实现淘宝客服功能的图文代码教程