Simple Search Concept using Ajax and php | Sanwebcorner

Simple Search Concept using Ajax and php

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" /> &nbsp; &nbsp; &nbsp; &nbsp;

            <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?

Reply

EDIT: This can make us Update the info inside the database.
ENABLE: This can make the user his account inaccesible. And after clicking the ENABLE button it will be replace with DISABLE button. (vice-versa)

Reply

Post a Comment

Labels

360 degree view product image 3d rotate 3d text 3step-form Accordion menu ajax All Browsers Animated Background Gradient animation audio controller auto complete Best accordion menu bootstrap border bounce effect on hover Breadcrumb calculate age by dob calendar Captcha change image on mouse move color chooser countdown Counter on scroll Css css animation css buttons css hover css3 customized upload Button Date picker delete data from table Different scroll speed email otp verification email verifier Embed eventlist Expanding Menu feedbck form flash news section close button full screen menu Google custom search Google Maps Google reCAPTCHA Google site search gradient hide menu Horizontal News ticker Hover Htm Html html dropdown html form html5 html5 form validation Iframe image hover increment decrement textbox value based on select box Javascript Jquery js Layered image effect on mouse hover lock & unlock page scroll marquee marquee with close button mobile number validation mobile otp login Multi-level-drop-down-menu multi-level-form Multi-Step-form mysql News ticker on hover change text to image our team page password strength checker Photoshop PHP populate html selectbox Popup image on click the link pure css Question type form Random password Redirect page Based on the select box while submit the form responsive responsive menu responsive popup box Responsive Vertical Time line design rotate image selectbox based on select box simple coming soon page simple countdown based on end date simple typing effect slider sliding tab sms otp login statistic counter on scroll switch tag shape team member Time picker toggle toggle view transparent button underline animation up down slide validation vertical fixed sidebar Webtips Youtube