使用代码点火器自动完成Ajax搜索 [英] Ajax Auto-complete search with Code-igniter

查看:91
本文介绍了使用代码点火器自动完成Ajax搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Ajax使用我的数据库中的Code-igniter自动完成搜索.我正在尝试搜索数据库,而Ajax从数据库中保存的项目中完成了搜索.我相信我缺少一个简单的把戏.也许我正在写我的控制器,或者一切都错了……下面的代码

Ajax Auto-complete search with Code-igniter from my database. I am trying to search my database and Ajax completes the search from items saved on my database. 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

<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

// My Model Location path: 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的新手.这说明了我相当明显的无知

Please be aware I am new to CodeIgniter. It explains my rather obvious ignorance

推荐答案

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

echo $data['ajaxdata'];

Ajax方法期望以(JSON)字符串形式的数据.因此,您无需再次加载标头.相反,只要从数据库传递所需的数据,jQuery就会将其放在指定的位置.在这种情况下,将其添加到具有搜索结果ID的元素中.

Ajax method expects data in form of (JSON) string. So you don't need to load header again. Instead, just pass needed data from DB and jQuery will put it in designated place. In this case into element with id of searchresults.

这篇关于使用代码点火器自动完成Ajax搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆