title: Live search using codeigniter Json Mysql Ajax Jquery
body: <p>I am doing a project on Live Search Using Jquery Ajax, Php Codeigniter and Mysql<br>
  This search is similar to 
  <a href="http://w3schools.com/ajax/ajax_aspphp.asp" rel="nofollow">http://w3schools.com/ajax/ajax_aspphp.asp</a> but with MYSql   </p>
  
  <p>Here is my database table:</p>
  
  <pre><code>CREATE TABLE IF NOT EXISTS `ix08s_subjects` (
    `SUB_ID` int(11) NOT NULL AUTO_INCREMENT,
    `NAME` varchar(211) DEFAULT NULL,
    `added_by` int(11) DEFAULT NULL,
    `date_added` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    PRIMARY KEY (`SUB_ID`),
    KEY `FK_ix08s_subjects` (`added_by`)
  ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 COMMENT='to store the subjects' AUTO_INCREMENT=3 ;
  </code></pre>
  
  <p>--</p>
  
  <h2>-- Dumping data for table <code>ix08s_subjects</code></h2>
  
  <pre><code>INSERT INTO `ix08s_subjects` (`SUB_ID`, `NAME`, `added_by`, `date_added`) VALUES
  (1, 'physics', 2, '2013-05-31 10:07:55'),
  (2, 'physics', 2, '2013-05-31 10:41:09');
  </code></pre>
  
  <p>Here is my model code-
  Subjectmodel.php</p>
  
  <pre><code>&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  class Subjectmodel extends CI_Model {
     public function __construct() {
          parent::__construct();
      }
      function getSubject($search){
          $this-&gt;db-&gt;select("SUB_ID,NAME");
          $whereCondition = array('NAME' =&gt;$search);
          $this-&gt;db-&gt;where($whereCondition);
          $this-&gt;db-&gt;from('ix08s_subjects');
          $query = $this-&gt;db-&gt;get();
          return $query-&gt;result();
      }
  }
  ?&gt;
  </code></pre>
  
  <p>Here is my Controller code-
  Subject.php</p>
  
  <pre><code>&lt;?php
  class Subject extends CI_Controller {
      function __construct() {
          parent::__construct();
          $this-&gt;load-&gt;model('SubjectModel');
      }
      public function index(){
          $search=  $this-&gt;input-&gt;post('search');
          $query = $this-&gt;SubjectModel-&gt;getSubject($search);
          echo json_encode ($query);
          $this-&gt;load-&gt;view('search', $query);
      }
  }
  ?&gt;
  </code></pre>
  
  <p>Here is my View code-
  Search.php</p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;
  
  &lt;script type="text/javascript" language="javascript" src="http://somexyz/js/javascripts/plugin/jquery.js"&gt;&lt;/script&gt;
  &lt;script type="text/javascript" src="http://somexyz/js/javascripts/plugin/json2.js"&gt;&lt;/script&gt;
  &lt;script&gt;
      $(document).ready(function(){
        $("#search").keyup(function(){
          if($("#search").val().length&gt;3){
          $.ajax({
              type: "post",
              url: "http://localhost/ibps/index.php/subject",
              cache: false,               
              data:'search='+$("#search").val(),
              dataType: 'json',
              success: function(response){
                  $('#finalResult').html("");
                  var obj = JSON.parse(response);
                  if(obj.length&gt;0){
                      try{
                          var items=[];   
                          $.each(obj, function(i,val){                                            
                              items.push($('&lt;li/&gt;').text(val.NAME));
                          }); 
                          $('#finalResult').append.apply($('#finalResult'), items);
                      }catch(e) {     
                          alert('Exception while request..');
                      }       
                  }else{
                      $('#finalResult').html($('&lt;li/&gt;').text("No Data Found"));       
                  }       
  
              },
              error: function(){                      
                  alert('Error while request..');
              }
          });
          }
          return false;
        });
      });
  &lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;div id="container"&gt;
  
  &lt;input type="text" name="search" id="search" /&gt;
  &lt;ul id="finalResult"&gt;&lt;/ul&gt;
  &lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>When ever I type four letters into the search box I get this alert pop up  </p>
  
  <p>Error while request..</p>
  
  <p>Not getting the values from the database</p>