Live Search Using Jquery Ajax, Php Codeigniter and Mysql-2

Most of the time we have to implement live search using ajax to lookup user directory or employee search. Today we are going to implement Jquery ajax live search. We are going to made Jquery ajax request after three characters and get the response in the form of json object and display the response content dynamically.

Create Database table

Create “trn_employee” table in mysql database using below sql table script and insert below records.

DROP TABLE IF EXISTS `technicalkeeda`.`trn_employee`;
CREATE TABLE  `technicalkeeda`.`trn_employee` (
  `employee_id` bigint(20) NOT NULL auto_increment,
  `first_name` varchar(255) collate latin1_general_ci default NULL,
  `last_name` varchar(255) collate latin1_general_ci default NULL,
  PRIMARY KEY  (`employee_id`)
INSERT INTO `trn_employee` (`employee_id`,`first_name`,`last_name`) VALUES 

Codeigniter Controller(Employee.php)

Create Employee Controller class having default index() method which made request to EmployeeModel and get the Employee result base on search query. Employee list result is converted into json object format.

class Employee extends Controller {

 function Employee(){

 public function index(){
  $search=  $this->input->post('search');
  $query = $this->EmployeeModel->getEmployee($search);
  echo json_encode ($query);

Model Class (EmployeeModel.php File)

Define Model class contains getEmployee() method which return employee.

class EmployeeModel extends Model {

 function EmployeeModel(){

 function getEmployee($search){
  $whereCondition = array('LAST_NAME' =>$search);
  $query = $this->db->get();
  return $query->result();

Json Response

echo json_encode ($query); converts the resultant employee list into json object format.



Capture the keyup() event and made the Jquery ajax request to Employee Controller. Jquery ajax request is made after 3 characters. And get the json response and display the employee list.

#search {
 background-color: lightyellow;
 outline: medium none;
 padding: 8px;
 width: 300px;
 border-radius: 2px;
 -moz-border-radius: 3px;
 -webkit-border-radius: 3px;
 border-radius: 3px;
 border: 2px solid orange;

ul {
 width: 300px;
 margin: 0px;
 padding-left: 0px;

ul li {
 list-style: none;
 background-color: lightgray;
 margin: 1px;
 padding: 1px;
 -moz-border-radius: 3px;
 -webkit-border-radius: 3px;
 border-radius: 3px;

<script type="text/javascript" language="javascript" src=""></script>
<script type="text/javascript" src=""></script>
   type: "post",
   url: "http://localhost/technicalkeeda/index.php/employee",
   cache: false,    
   success: function(response){
    var obj = JSON.parse(response);
      var items=[];  
      $.each(obj, function(i,val){           
          items.push($('<li/>').text(val.FIRST_NAME + " " + val.LAST_NAME));
      $('#finalResult').append.apply($('#finalResult'), items);
     }catch(e) {  
      alert('Exception while request..');
     $('#finalResult').html($('<li/>').text("No Data Found"));  
   error: function(){      
    alert('Error while request..');
  return false;
<div id="container">
<p>Note:- Please start typing surname as "Chavan", "Patil"</p>
<input type="text" name="search" id="search" />
<ul id="finalResult"></ul>
(Visited 56 times, 1 visits today)



Leave a Reply