doutao4480 2014-10-27 05:24
浏览 45
已采纳

CodeIgniter活动记录和AJAX返回所有结果并忽略关键字

I'm trying to implement a "live search" function for my web app. Basic idea is very simple: user type's in keyword and AJAX will handle the request and returns all the rows from the database that matched the keyword. However, every time I type in something in my search form, it will return all the results and completely bypasses the actual keyword (like part).

I have tried to get this to work for hours now and can't really figure out the problem. I've tried with and without active record, it always gives me all the results from the database.

These are the only SQL statements it will consider:

$this->db->where('event_date >=', date('Y-m-d G:i:s'));
$this->db->where('visibility', 1);

Here's my View:

<!-- Search form -->
<?php $attributes = ['id' => 'search', 'autocomplete' => 'off'];
echo form_open('ajax/search', $attributes);?>
    <input type="text" id="app-search" name="keyword" class="search-field" placeholder="Hae tapahtumaa nimellä tai paikkakunnalla...">
    <i class="fa fa-search search-icon"></i>
    <input type="submit" name="search" class="search-button" value="Hae">
    <?php echo form_close();?>

<!-- div for the results -->
<div class="ajax-search-results"> 
        <div class="spinner-for-search" id="search-list">
           <i class="fa fa-circle-o-notch fa-2x fa-spin" id="ajax-loading-search" style="text-align: center;"></i>
        </div>
</div>

Here's my JS (for AJAX request):

$(".ajax-search-results").hide();
    $("#app-search").on("keyup", function(e) {
        if($("#app-search").val().length > 5) {

            $(".ajax-search-results").show();
            $("#ajax-loading-search").show();

            $.ajax({
                type: "post",
                url: $("#app-search").parent('form').attr('action'),
                cache: false,               
                data:'search='+$("#app-search").val(),
                success: function(response) {
                    $('.ajax-search-results').html("");
                    var obj = JSON.parse(response);
                    if(obj.length > 0){ 
                        try{
                            var items=[];   
                            $.each(obj, function(i,val){                                            
                                items.push($('<li/><a href=' + window.location.origin + '/Asiakastyot/STCodeIgniter/event/'+ val.event_id +'/>').text(val.event_name + "-" + val.event_city));
                            }); 
                            $('.ajax-search-results').append.apply($('.ajax-search-results'), items);
                        }catch(e) {     
                            alert('Tapahtui virhe, yritä uudelleen tai ota yhteyttä palveluntarjoajaan...');
                        }       
                    }else{

                        $('.ajax-search-results').html($('<li/>').text("Ei hakutuloksia"));       
                    }       

                },
                error: function(){                      
                    alert('Error while request..');
                },
                complete: function() {

                    $("#ajax-loading-search").hide();
                }
            });
}
return false;
});

Here's my model:

<?php

class Search_model extends CI_Model {

    public function ajaxSearch($keyword)
    {   

        $this->db->select('event_id, event_name, event_city')->from('events');
        $this->db->like('event_name', $keyword);
        $this->db->where('event_date >=', date('Y-m-d G:i:s'));
        $this->db->where('visibility', 1);

        $query = $this->db->get();

        if($query->num_rows() >= 1) {

            return $query->result();
        }
    }

}

And finally the controller:

<?php 
class Ajax extends CI_Controller {

    public function search() {

        $this->load->model('search_model');

        $search =  $this->input->post('keyword');
        $query  =  $this->search_model->ajaxSearch($search);
        echo json_encode($query);
    }
}
  • 写回答

1条回答 默认 最新

  • donglin7383 2014-10-28 10:17
    关注

    In Jquery you send data with name as 'search'

    data:'search='+$("#app-search").val(),
    

    but in model you are trying to get result from 'keyword'

     $search =  $this->input->post('keyword');
    

    change to

      $search =  $this->input->post('search');
    

    and everything should work.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题