title: Creating live search with AJAX and CodeIgniter
body: <p>I'm trying to create a live search feature in CI (which I've never done before). I'm pretty new to web development and am still learning things. I found this little tutorial on how to do it <a href="http://www.blog.orionwebtech.net/codeigniter-jquery-ajax-live-search/?codekitCB=415037771.748888" rel="nofollow">http://www.blog.orionwebtech.net/codeigniter-jquery-ajax-live-search/?codekitCB=415037771.748888</a></p>
  
  <p>I'm having issues translating the code from that tutorial for my app. I have a table called properties and I want the input value to be compared against the slug column and the name column. And then I want it to return the slug and the name in the live search results where the input value matches either of those columns. It wont match both because the slug contains only numbers and the name contains letters.</p>
  
  <p>This is the code I came up with to try and do this.</p>
  
  <p>The View:</p>
  
  <pre><code>&lt;div class="something"&gt;
  
      &lt;input name="search_data" id="search_data" class="" value="" data-label="Search for a property, a unit, or an resident..." type="text" /&gt;
  
      &lt;div id="suggestions"&gt;
  
          &lt;div id="suggestionslist"&gt;
  
          &lt;/div&gt;
  
      &lt;/div&gt;
  
  &lt;/div&gt;
  </code></pre>
  
  <p>The JavaScript:</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
          function ajaxSearch() {
              var input_data = $('#search_data').val();
              if (input_data.length === 0) {
                  $('#suggestions').hide();
              } else {
  
                  var post_data = {
                      'search_data': input_data,
                      '&lt;?php echo $this-&gt;security-&gt;get_csrf_token_name(); ?&gt;': '&lt;?php echo $this-&gt;security-&gt;get_csrf_hash(); ?&gt;'
                  };
  
                  $.ajax({
                      type: "POST",
                      url: "&lt;?php echo base_url(); ?&gt;search/autocomplete",
                      data: post_data,
                      success: function(data) {
                          // return success
                          if (data.length &gt; 0) {
                              $('#suggestions').show();
                              $('#autoSuggestionsList').addClass('auto_list');
                              $('#autoSuggestionsList').html(data);
                          }
                      }
                  });
  
              }
          }
  &lt;/script&gt;
  </code></pre>
  
  <p>The Controller:</p>
  
  <pre><code>&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  
  class Search extends CI_Controller
  {
      public function __construct()
      {
          parent::__construct();
      }
  
      public function autocomplete()
      {
          $search_data = $this-&gt;input-&gt;post('search_data');
          $query = $this-&gt;Search_model-&gt;get_autocomplete($search_data);
  
          foreach ($query-&gt;result() as $row):
              echo "&lt;li&gt;&lt;a href='" . base_url('association/'.$row-&gt;slug) . "'&gt;" . $row-&gt;name . "&lt;/a&gt;&lt;/li&gt;";
          endforeach;
      }
  }
  /* End of file search.php */
  /* File location: application/controllers */
  </code></pre>
  
  <p>The Model:</p>
  
  <pre><code>&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  
  class Search_model extends CI_Model
  {
      public function __construct()
      {
          $this-&gt;load-&gt;database();
      }
  
      public function get_autocomplete($search_data)
      {
          $this-&gt;db-&gt;select('slug, name');
          $this-&gt;db-&gt;like('slug', $search_data);
          $this-&gt;db-&gt;like('name', $search_data);
          return $this-&gt;db-&gt;get('properties', 10);
      }
  }
  
  ?&gt;
  </code></pre>
  
  <p>When I tested it I didnt get any results. My test data is valid because there is a matching row in the db. What am I doing wrong?</p>