Saturday, March 1, 2008 |
Data Retrieve from Mysql using AJAX with PHP |
This example is useful to those programmers who want to use ajax in php.
Tested on Browse Name: Opera / 8.53 Browse Name: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.2) Gecko/20060308 Firefox/1.5.0.2 Browse Name: Microsoft Internet Explorer / 6.0 Browse Name : Mozilla 1.5
GetCustomerData.php
| <html> <head> <title>Get Customer Data</title> //customer ID $sID = $_GET["id"]; //variable to hold customer info $sInfo = ""; //database information $sDBServer = "your_server_name"; $sDBName = "your_database_name"; $sDBUsername = "your_user_name"; $sDBPassword = "your_password";
//create the SQL query string $sQuery = "Select * from Customers where CustomerId=".$sID; //make the database connection $oLink = mysql_connect($sDBServer,$sDBUsername,$sDBPassword); @mysql_select_db($sDBName) or $sInfo = "Unable to open database"; if($sInfo == '') { if($oResult = mysql_query($sQuery) and mysql_num_rows($oResult) > 0) { $aValues = mysql_fetch_array($oResult,MYSQL_ASSOC); $sInfo = $aValues['Name']." ".$aValues['Address']." ". $aValues['City']." ".$aValues['State']." ". $aValues['Zip']."
Phone: ".$aValues['Phone']." ". ".$aValues['E-mail']."\">".$aValues['E-mail'].""; } else { $sInfo = "Customer with ID $sID doesn't exist."; } } mysql_close($oLink);
?>
</head> <body> echo $sInfo ?> &;lt/body> </html> |
|
display.htm
|
<html> <head> <title>Customer Account Information</title> <script type="text/javascript"> var url = "GetCustomerData.php?id="; // The server-side script function handleHttpResponse() { if (http.readyState == 4) { if(http.status==200) { var results=http.responseText; document.getElementById('divCustomerInfo').innerHTML = results; } } } function requestCustomerInfo() { var sId = document.getElementById("txtCustomerId").value; http.open("GET", url + escape(sId), true); http.onreadystatechange = handleHttpResponse; http.send(null); } function getHTTPObject() { var xmlhttp;
if(window.XMLHttpRequest){ xmlhttp = new XMLHttpRequest(); } else if (window.ActiveXObject){ xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); if (!xmlhttp){ xmlhttp=new ActiveXObject("Msxml2.XMLHTTP"); } } return xmlhttp;
} var http = getHTTPObject(); // We create the HTTP Object </script> </head> <body> <p>Enter customer ID number to retrieve information:</p> <p>Customer ID: <input type="text" id="txtCustomerId" value=""></p> <p><input type="button" value="Get Customer Info" onclick="requestCustomerInfo()"></p> <div id="divCustomerInfo"></div> </body> </html> |
|
customers.txt
| -- phpMyAdmin SQL Dump -- version 2.6.0-pl3 -- -- Host: localhost -- Generation Time: Apr 30, 2006 at 05:45 PM -- Server version: 4.1.8 -- PHP Version: 5.0.3 -- -- Database: `ajax_ex` --
-- --------------------------------------------------------
-- -- Table structure for table `customers` --
CREATE TABLE `customers` ( `CustomerId` int(11) NOT NULL auto_increment, `Name` varchar(255) NOT NULL default '', `Address` varchar(255) NOT NULL default '', `City` varchar(255) NOT NULL default '', `State` varchar(255) NOT NULL default '', `Zip` varchar(255) NOT NULL default '', `Phone` varchar(255) NOT NULL default '', `E-mail` varchar(255) NOT NULL default '', PRIMARY KEY (`CustomerId`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 COMMENT='Sample Customer Data';
-- -- Dumping data for table `customers` --
INSERT INTO `customers` VALUES (1, 'shankar das', 'indrapuri', 'bhopal', 'bpl', '462021', '91-9893312345', 'shankardas76@gmail.com'); INSERT INTO `customers` VALUES (2, 'shankar das (dcs)', 'indrapuri', 'bhopal', 'bpl', '462021', '91-9893312345', 'shankardas76@gmail.com');
|
Labels: AJAX, MySQL, PHP |
posted by WebTeks @ 3:44 AM |
|
|