Ajax使用Code-igniter自动完成搜索

时间:2015-12-11 02:44:57

标签: php jquery ajax codeigniter

Ajax使用我的数据库中的Code-igniter自动完成搜索。我正在尝试搜索我的数据库,Ajax从保存在我的数据库中的项目完成搜索。我相信我错过了一个简单的伎俩。也许我正在编写我的控制器或者可能一切都错了......下面的代码

//查看页面 位置路径:application / views / template / header

<form class="navbar-form" >
                        <input type="text" id="mysearch" placeholder="search" onkeyup="doSearch();">                        
                        <br />

<script>

   // This is the jQuery Ajax call
   function doSearch()
   {
      $.ajax({
         type: "GET",
         url:"localhost/codeigniter/index.php/ajax/getdata/" + $("#mysearch").val(),
         success:function(result){
         $("#searchresults").html(result);
      }});
   }
   //class example



</script>

Note: My form or search box is inside my header... So my view page is located in template/header

// Controller Page

Location path: codeigniter/application/controller/ajax.php

class Ajax extends CI_Controller 
{
        public function __construct()
        {
                parent::__construct();
                $this->load->model('ajax_model');
                //$this->load->helper('url_helper');
        }

        public function form ()
        {
            $data['title'] = 'Ajax search';

            $this->load->view('template/header');
        }


        // function ends

    public function getdata($param = '')
   {
      // Get data from db 
      $data['ajaxdata'] = $this->ajax_model->search($param);

      // Pass data to view
     $this->load->view('template/header', $data);


   }

}

?>

//我的模特    位置路径:application / model / Ajax_model.php

<?php if (! defined('BASEPATH')) exit('No direct script access');

class Ajax_model extends CI_Model
{
    public function __construct()
        {
                $this->load->database();
        }

    public function search ($title){
        $this->db->select('title');
        $this->db->select('text');
        $this->db->like('title', $title, 'both');
        return $this->db->get('news');
    }

}

?>

请注意我是CodeIgniter的新手。它解释了我相当明显的无知

4 个答案:

答案 0 :(得分:0)

尝试更改此

$this->load->view('template/header', $data);

$content = $this->load->view('template/header', $data,TRUE);
// load view to a variable.
echo $content;

答案 1 :(得分:0)

$data['ajaxdata'] = $this->ajax_model->search($param);
$data['ajaxdata'] = json_encode($data['ajaxdata']);

echo $data['ajaxdata'];

Ajax方法需要(JSON)字符串形式的数据。所以你不需要再次加载标题。相反,只需从DB传递所需的数据,jQuery就会将其放在指定的位置。在这种情况下,使用id为searchresults的元素。

答案 2 :(得分:0)

如果我清楚你需要尝试: 首先定义ajax请求类型:

function doSearch()
   {
      $.ajax({
         type: "GET",
         dataType:"html",
         url:"localhost/codeigniter/index.php/ajax/getdata/" + $("#mysearch").val(),
         success:function(result){
         $("#searchresults").html(result);
      }});
   }
  

然后在控制器中:

回应你的观点:

$auto_complete_html = $this->load->view('template/header', $data,TRUE);
echo $auto_complete_html;
//good practice always die(); after ajax called 
die();

答案 3 :(得分:0)

尝试在AJAX中使用POST而不是GET:

<script>

   // This is the jQuery Ajax call
   function doSearch()
   {
      var search = $("#mysearch").val()
      $.ajax({
         type: "POST",
         url:"localhost/codeigniter/ajax/getdata/",
         data:'search=' + search,
         success:function(data){
         $("#searchresults").html(data);
      }});
   }
   //class example

</script>

然后在你的控制器中从AJAX获取POSTED数据

    public function getdata()
   {
$param= $this->input->post('search');
      // Get data from db 
     $result = $this->ajax_model->search($param);

      // Pass data to view
 echo $result; 


   }