Today, We want to share with you select2 codeigniter.In this post we will show you CodeIgniter Autocomplete Search from Database using AJAX Example, hear for Codeigniter 3 – select2 ajax autocomplete from database example with demo we will give you demo and example for implement.In this post, we will learn about
codeigniter autocomplete from database with an example.
Loading data remotely in Select2 in CodeIgniter
application/views/welcome_message.php
<html lang="en"> <head> <title>Jquery select2 ajax autocomplete example code with demo - www.pakainfo.com</title> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.js"></script> <link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.min.css" rel="stylesheet" /> <script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js"></script> </head> <body> <div style="width:520px;margin:0px auto;margin-top:30px;height:500px;"> <h2>Select Box with Search Option Jquery Select2.js</h2> <select class="memberName form-control" style="width:500px" name="memberName"></select> </div> <script type="text/javascript"> $('.memberName').select2({ placeholder: '--- Select Member ---', ajax: { url: '/search', dataType: 'json', delay: 250, processResults: function (data) { return { results: data }; }, cache: true } }); </script> </body> </html>
Create Route
application/config/routes.php
$route['default_controller'] = 'welcome'; $route['404_override'] = ''; $route['translate_uri_dashes'] = FALSE; $route['search'] = "search/index";
Create Controller
application/controllers/Search.php
defined('BASEPATH') OR exit('No direct script access allowed'); class Search extends CI_Controller { public function index() { $json = []; $this->load->database(); if(!empty($this->input->get("q"))){ $this->db->like('name', $this->input->get("q")); $query = $this->db->select('id,name as text') ->limit(10) ->get("products"); $json = $query->result(); } echo json_encode($json); } }
I hope you get an idea about autocomplete textbox in jquery example using database in codeigniter.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.
I am Jaydeep Gondaliya , a software engineer, the founder and the person running Pakainfo. I’m a full-stack developer, entrepreneur and owner of Pakainfo.com. I live in India and I love to write tutorials and tips that can help to other artisan, a Passionate Blogger, who love to share the informative content on PHP, JavaScript, jQuery, Laravel, CodeIgniter, VueJS, AngularJS and Bootstrap from the early stage.