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.

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

报告相同问题?

悬赏问题

  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址
  • ¥15 elmos524.33 eeprom的读写问题
  • ¥15 使用Java milo连接Kepserver服务端报错?
  • ¥15 用ADS设计一款的射频功率放大器
  • ¥15 怎么求交点连线的理论解?
  • ¥20 软件开发方法学习来了
  • ¥15 微信小程序商城如何实现多商户收款 平台分润抽成