AJAX Stands for Asynchronous Javascript and xml. AJAX is one the technique for creating a better and faster dynamic sites. In this concept i am explain about a Simple Search Concept using Ajax and php and mysql database.
This simple search concept is used search the data from database using name and then retrieve the data from database in table. I am using here AJAX Concept so that it will work faster.
This simple ajax search concept i given the key instruction to the server side script with the help of java script this called as a AJAX.
Source Code:
Databse and Table:
create database `sanwebcorner`;
CREATE TABLE IF NOT EXISTS `emp` (
`EmpID` int(11) NOT NULL AUTO_INCREMENT,
`EmpName` varchar(150) NOT NULL,
`Designation` varchar(150) NOT NULL,
`Age` varchar(5) NOT NULL,
`Salary` varchar(8) NOT NULL,
PRIMARY KEY (`EmpID`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=17 ;
index.php
<html>
<head>
<style type="text/css">
body {
margin: 0px;
}
.head {
margin-top: 0px;
background-color: #DDB744;
height: 70px;
}
.center {
width: 800px;
margin-top: 50px;
margin-left: auto;
margin-right: auto;
border: 1px solid;
padding: 55px;
border-color: #CCC;
border-radius: 12px;
height: auto;
letter-spacing: 1px;
font-size: 19px;
}
.lab {
height: 38px;
width: 450px;
margin-left: 45px;
}
.search {
height: 38px;
width: 100px;
background-color: #DDB744;
}
.foot {
margin-top: 400px;
background-color: #DDB744;
height: 70px;
}
</style>
</head>
<body>
<script language="javascript" type="text/javascript">
function ajaxFunction() {
var ajaxRequest;
try {
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
} catch (e) {
// Internet Explorer Browsers
try {
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {
alert("Your browser broke!");
return false;
}
}
}
ajaxRequest.onreadystatechange = function() {
if (ajaxRequest.readyState == 4) {
var ajaxDisplay = document.getElementById('ajaxDiv');
ajaxDisplay.innerHTML = ajaxRequest.responseText;
}
}
var EmpName = document.getElementById('EmpName').value;
var queryString = "?EmpName=" + EmpName;
ajaxRequest.open("GET", "ajax.php" +
queryString, true);
ajaxRequest.send(null);
}
</script>
<div class="head">
<center>
<h1>http://www.sanwebcorner.com/</h1>
</center>
</div>
<div class="center">
<form name='myForm'>
Name:
<input type='text' id='EmpName' class="lab" />
<input type='button' class="search" onclick='ajaxFunction()' value='Search' />
</form>
<div id='ajaxDiv'> Your Result Display Here</div>
</div>
<div class="foot">
<center>
<h3><br>http://www.sanwebcorner.com/</h3>
</center>
</div>
</body>
</html>
<head>
<style type="text/css">
body {
margin: 0px;
}
.head {
margin-top: 0px;
background-color: #DDB744;
height: 70px;
}
.center {
width: 800px;
margin-top: 50px;
margin-left: auto;
margin-right: auto;
border: 1px solid;
padding: 55px;
border-color: #CCC;
border-radius: 12px;
height: auto;
letter-spacing: 1px;
font-size: 19px;
}
.lab {
height: 38px;
width: 450px;
margin-left: 45px;
}
.search {
height: 38px;
width: 100px;
background-color: #DDB744;
}
.foot {
margin-top: 400px;
background-color: #DDB744;
height: 70px;
}
</style>
</head>
<body>
<script language="javascript" type="text/javascript">
function ajaxFunction() {
var ajaxRequest;
try {
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
} catch (e) {
// Internet Explorer Browsers
try {
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {
alert("Your browser broke!");
return false;
}
}
}
ajaxRequest.onreadystatechange = function() {
if (ajaxRequest.readyState == 4) {
var ajaxDisplay = document.getElementById('ajaxDiv');
ajaxDisplay.innerHTML = ajaxRequest.responseText;
}
}
var EmpName = document.getElementById('EmpName').value;
var queryString = "?EmpName=" + EmpName;
ajaxRequest.open("GET", "ajax.php" +
queryString, true);
ajaxRequest.send(null);
}
</script>
<div class="head">
<center>
<h1>http://www.sanwebcorner.com/</h1>
</center>
</div>
<div class="center">
<form name='myForm'>
Name:
<input type='text' id='EmpName' class="lab" />
<input type='button' class="search" onclick='ajaxFunction()' value='Search' />
</form>
<div id='ajaxDiv'> Your Result Display Here</div>
</div>
<div class="foot">
<center>
<h3><br>http://www.sanwebcorner.com/</h3>
</center>
</div>
</body>
</html>
ajax.php
<?php
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "";
$dbname = "sanwebcorner";
//Connect to MySQL Server
mysql_connect($dbhost, $dbuser, $dbpass);
//Select Database
mysql_select_db($dbname) or die(mysql_error());
// Retrieve data from Query
$EmpName = $_GET['EmpName'];
// Escape User Input to help prevent SQL Injection
$EmpName = mysql_real_escape_string($EmpName);
$query = "SELECT * FROM emp WHERE EmpName = '$EmpName'";
$qry_result = mysql_query($query) or die(mysql_error());
$display_string = "<table border='1' cellpadding='10' cellspacing='1' align='center'>";
$display_string .= "<tr>";
$display_string .= "<th>Name</th>";
$display_string .= "<th>Designation</th>";
$display_string .= "<th>Age</th>";
$display_string .= "<th>Salary</th>";
$display_string .= "</tr>";
// Insert a new row in the table for each person returned
while($row = mysql_fetch_array($qry_result)){
$display_string .= "<tr>";
$display_string .= "<td>$row[EmpName]</td>";
$display_string .= "<td>$row[Designation]</td>";
$display_string .= "<td>$row[Age]</td>";
$display_string .= "<td>$row[Salary]</td>";
$display_string .= "</tr>";
}
$display_string .= "</table>";
echo $display_string;
?>
2 Comments
After searching the data. Can we add Edit and Enable button?
ReplyDeleteEDIT: This can make us Update the info inside the database.
ReplyDeleteENABLE: This can make the user his account inaccesible. And after clicking the ENABLE button it will be replace with DISABLE button. (vice-versa)