简体   繁体   English

带代码点火器的Ajax自动完成搜索

[英]Ajax Auto-complete search with Code-igniter

Ajax Auto-complete search with Code-igniter from my database. 使用我的数据库中的Code-igniter自动完成Ajax搜索。 I am trying to search my database and Ajax completes the search from items saved on my database. 我正在尝试搜索数据库,而Ajax从数据库中保存的项目中完成了搜索。 I believe I am missing a simple trick. 我相信我缺少一个简单的把戏。 Maybe I am writing my controller or maybe everything all wrong... Code below 也许我正在写我的控制器,或者一切都错了……下面的代码

// View Page Location path: application/views/template/header //查看页面位置路径: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);


   }

}

?>

// My Model Location path: application/model/Ajax_model.php //我的模型位置路径: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');
    }

}

?>

Please be aware I am new to CodeIgniter. 请注意,我是CodeIgniter的新手。 It explains my rather obvious ignorance 这解释了我相当明显的无知

Try changing this 尝试改变这个

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

to

$content = $this->load->view('template/header', $data,TRUE);
// load view to a variable.
echo $content;
$data['ajaxdata'] = $this->ajax_model->search($param);
$data['ajaxdata'] = json_encode($data['ajaxdata']);

echo $data['ajaxdata'];

Ajax method expects data in form of (JSON) string. Ajax方法期望以(JSON)字符串形式的数据。 So you don't need to load header again. 因此,您无需再次加载标头。 Instead, just pass needed data from DB and jQuery will put it in designated place. 取而代之的是,仅传递来自DB的所需数据,jQuery会将其放置在指定的位置。 In this case into element with id of searchresults. 在这种情况下,将其转换为具有搜索结果ID的元素。

if i am clear what you need try: first define ajax request type: 如果我清楚您需要什么,请尝试:首先定义ajax请求类型:

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

Then in controller : 然后在控制器中:

just echo your view: 只是回应您的观点:

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

Try using POST in AJAX instead of GET: 尝试在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>

Then in your controller Get THE POSTED data from AJAX 然后在您的控制器中从AJAX获取发布的数据

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

      // Pass data to view
 echo $result; 


   }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM