CodeIgniter Forums

Full Version: Live Search Not Working! Ayuda Me! Help!
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Okay... So I'm trying to do a fancy ajax on key press search thing and I'm using CodeIgniter. All in all my code has no errors but nothing appears when I search.


*** View - home.php

Code:
<input name="search_data" id="search_data" class="" value="" data-label="Search for a property, a unit, or an resident..." type="text"  onkeydown="ajaxSearch();" /><br>
  <div id="suggestionslist"></div>

 <script type="text/javascript">
   function ajaxSearch() {
       var input_data = $('#search_data').val();
       if (input_data.length < 0) {

           alert("length==0");
       } else {

           var post_data = {
               'search_data': input_data,
               '<?php echo $this->security->get_csrf_token_name(); ?>': '<?php echo $this->security->get_csrf_hash(); ?>'
           };

           $.ajax({
               type: "POST",
               url: "<?php echo base_url(); ?>home/autocomplete",
               data: post_data,
               success: function(data) {
                   // return success
                   if (data.length > 0) {
                       alert("Here");
                       $('#autoSuggestionsList').html(data);
                       alert("Here below");
                   }
               }
           });}}

*** Controller - home.php

Code:
public function autocomplete()
       {
           $search_data = $this->input->post('search_data');
           $query = $this->home_model->get_autocomplete($search_data);
           foreach ($query->result() as $row):
           $edit = base_url().'home/edit/';
           $delete = base_url().'home/delete/';
           echo "<tr>
                       <td>$row->fullname</td>
                       <td>$row->username</td>
                       <td>$row->type</td>
                       <td><a href='$edit' data-id='$row->user_id' class='btnedit' title='edit'><i class='glyphicon glyphicon-pencil' title='edit'></i></a>&nbsp;&nbsp;&nbsp;&nbsp;<a href='$delete' data-id='$row->user_id' class='btndelete' title='delete'><i class='glyphicon glyphicon-remove'></i></a></td>    
                   </tr>";
           endforeach; }
       


*** Model - home_model.php

Code:
    public function get_autocomplete($search_data)
   {
       $this->db->select('fullname, username');
       $this->db->or_like("fullname", $search_data);
       $this->db->or_like("username", $search_data);
       return $this->db->get('user_account', 10);

   }
Rather than one of us going through all your code and trying to guess where the problem might be, why don't you tell us where the problem is?

You are trying to send AJAX requests to a CodeIgniter controller and perform a query, right?

Does the request get sent from your page?
Does the controller receive the request?
Is the query being perfomed?
Is the result of the query as you would expect?

These are all things you should be able to answer with some simple debugging.

Use the Web Console or Dev Tools of your browser to see if the AJAX request is being sent (right click -> inspect element -> network tab)

Use CodeIgniter logging in your controller and model to trace the progress of the request and response.

These are things you need to be able to do eventually, so you may as well start now.
Code:
$this->db->select('fullname, username');
       $this->db->or_like("fullname", $search_data);
       $this->db->or_like("username", $search_data);
       ...

See what SQL actually is generated. If you replace the first or_like with like it might work.