使用ajax的Codeigniter自动搜索

时间:2015-07-23 02:44:27

标签: php ajax codeigniter

当我回应出来时,

ajax呼叫不是进入控制器的   =========== Ajax自动完成搜索========================

<script type="text/javascript" src="js/jquery-1.8.0.min.js"></script>
<script type="text/javascript">
$(function(){
$(".search").keyup(function() 
{ 
var searchid = $(this).val();
//var dataString = 'search='+ searchid;
if(searchid!='')

{
    $.ajax({
    type: "POST",
    url: "<?php echo base_url()."/search/search"?>",
    data: {searchid:searchid}
    cache: false,
    success: function(html)
    {
        alert(html);
    $("#result ").html(html).show();
    }
    });
}return false;    
});

$("#result ").live("click",function(e){ 
    var $clicked = $(e.target);
    var $name = $clicked.find('.name').html();
    var decoded = $("<div/>").html($name).text();
    $('#searchid').val(decoded);
});
$(document).live("click", function(e) { 
    var $clicked = $(e.target);
    if (! $clicked.hasClass("search")){
    $("#result").fadeOut(); 
    }
});
$('#searchid').click(function(){
    $("#result").fadeIn();
});
});

</script>

=============================控制器================= ======

public function search()
{
    //$q = mysql_real_escape_string($_POST['search']);
    //echo"helloooo".$q;
    //die();
      $searchid=$this->input->post('searchid');
      echo "aaaaaaa".$searchid;
      die();
        //echo "name".$name;
        //die();
       $data=$this->search_model->autocomplete($q);        
       // print_r($data);
    }  

}

=============================模型================= ================

public function autocomplete($q)
{
     $this->db->select('uid', 'name');
       $this->db->from('tbl_reg');
       // $this->db->where('suppress', 0);
        $this->db->like('name', $q,'both' );
        $this->db->order_by("name", "asc");

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

        //echo "query".$query;
        //die();


        //  $query=$this->db->query("select *  from  tbl_reg where name='$name' ");

        /**  
          $whereCondition = array('name' =>$search);
        $this->db->where($whereCondition);
        $this->db->from('tbl_reg');
        $query = $this->db->get();
        return $query->result();
          **/



        if($query->num_rows()>0)
    {


    foreach($query->result() as $row)
        {
          $data[] = $row;
        }

        //echo "query".$query;
        //die();
       // foreach($query->result_array() as $row){

          //  $data[] = $row;
       // }
        //return $data;
        return $query;
    }  
 }
 }

2 个答案:

答案 0 :(得分:0)

希望这有帮助。

查看

<link rel="stylesheet"href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.1/themes/base/minified/jquery-ui.min.css" type="text/css" /> 

  //Also include this script file
 //http://code.jquery.com/ui/1.10.1/jquery-ui.min.js

$(function () {

    //autocomplete
    $(".auto").autocomplete({
       source: "<?php echo base_url(); ?>search/auto_search",
        minLength: 1
    });

});

<input name="" id="" type="text" class="auto"/>

=============控制器============

public function auto_search() {
    if (isset($_GET['term'])) {
        $data = $this->Search_model->autocomplete(urldecode($_GET['term']));
        $json_array = array();
        foreach ($data as $row)
            array_push($json_array, $row->name);
        echo json_encode($json_array);
    }
}

===================模型==================

public function autocomplete($like) {

    $this->db->select('name');
    $this->db->from('tbl_reg');
   // $this->db->where('suppress', 0);
    $this->db->like('name', $like );
    $this->db->order_by("name", "asc");
    $q = $this->db->get();
    if ($q->num_rows() > 0) {
        return $q->result();
    }
}

希望你能找到这个,让我们知道结果。

答案 1 :(得分:0)

您创建了一个名为search的控制器,即您的班级名称为Search,并调用名为search的函数。如果您使用类名创建任何函数,它将是您的构造函数。因此,建议您将方法名称search更改为其他任何内容。