使用codeigniter ajax jquery自动建议文本字段

时间:2015-01-26 01:00:10

标签: php jquery ajax codeigniter

我有一个搜索表单,用户可以在文本字段中键入特定的类别,但我想让它更加用户友好,这就是为什么我希望该字段具有自动推荐功能,建议将来自mysql数据库。现在,我使用codeigniter AJAX / JQuery来实现它。问题是当我在文本字段上输入内容时,我会收到以下错误。

Fatal error: Call to a member function entries() on a non-object in /home/name/public_html/portal/reports/application/controllers/home_members.php on line 1089

以下是我的观点:

  <div class="control-group">
       <label class="control-label">Category Name</label>
       <div class="controls">
       <?php
       $params = array(
                 'id'            => 'category',
                 'name'          => 'category-name',
                 'placeholder'   => 'Type in the name of the category',
                 'class'         => 'input-xxlarge',
                 'autocomplete'  => 'off'
       );
       echo form_input($params);
       ?>
       </div>
       <div id="category-suggestions">
            <div class="suggestions" id="category-autoSuggestionsList">    

            </div>
       </div>
       <span class="help-block autocomplete">Start typing the category name. There will be suggestions under a pop-up list. The suggestions list will appear as you write the category name. If your category is found between the suggestions, you can directly click it.</span> 
  </div

&GT;

这是我在视图中的脚本

<script src="<?php echo base_url(); ?>assets1/js/jquery-1.7.2.min.js"></script>

<script type="text/javascript">
$(document).ready(function() {
     var item1 = '#category-suggestions';
     var item2 = '#category';
     var item3 = '#category-autoSuggestionsList';

     $(item1).hide();

     function lookup(fieldSuggestions, fieldSuggestionsList, inputString) {
          if(inputString.length == 0) {
               $(fieldSuggestions).hide();
          } else {             
               $.post("http://www.mydomain.com.ph/portal/reports/home_members/search_autocomplete", 
               {queryString: ""+inputString+""}, 
               function(data){
                    if(data.length >0) {
                         $(fieldSuggestions).show();
                         $(fieldSuggestionsList).html(data);
                    }
           });
          }
     }

     function fill(fieldId, fieldSuggestions, thisValue) {
          $(fieldId).val(thisValue);
          setTimeout("$('" + fieldSuggestions + "').hide();", 200);
     }

     $(item2).keyup(function() {
          lookup(item1, item3, $(item2).val());
     });

     $(item3 + " li").live('click', function() { 
          fill(item2, item1, $(this).attr('title'));         
     });
});
</script>

这是我的控制人员(home_members)

public function search_autocomplete() {
    $this->load->model('customers');
    $query = $this->customers->entries();

    foreach($query->result() as $row):
        echo "<li title='" . $row->category_name  . "'>" . $row->category_name  . "</li>";
    endforeach;    
}

最后是模型(客户)

 public function entries() {
      $this->db->select('customer_firstname');
      $this->db->like('category_name', $this->input->post('queryString'), 'both'); 
      return $this->db->get('customers', 10);  
 }

0 个答案:

没有答案