Skip to content
  • Github
  • Facebook
  • twitter
  • About Us
  • Contact Us
  • Privacy Policy
  • Terms & Conditions
  • Site Map

XpertPhp

Expertphp Is The Best Tutorial For Beginners

  • Home
  • Javascript
    • Jquery
    • React JS
    • Angularjs
    • Angular
    • Nodejs
  • Codeigniter
  • Laravel
  • Contact Us
  • About Us
  • Live Demos
Autocomplete Textbox in CodeIgniter using Typeahead

Autocomplete Textbox in CodeIgniter using Typeahead

Posted on June 4, 2018December 17, 2022 By XpertPhp No Comments on Autocomplete Textbox in CodeIgniter using Typeahead

In this article, We will explain how to Autocomplete Textbox in CodeIgniter using Typeahead. We can easily make Autocomplete Textbox using Typeahead in CodeIgniter Framework.

Read Also:
Codeigniter 4 Google Autocomplete Address Search Box Example

so, you can follow the below steps.

Download and install Codeigniter

We are going to install Codeigniter 3, first, we will download a fresh Codeigniter 3 version and install it in our system. so you can follow below Link for Download CodeIgniter.

Create a Database in table

In this step, we have to create a table in the database, so we will create a database using the below code.

1
2
3
4
5
6
7
8
9
CREATE TABLE IF NOT EXISTS `users` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `first_name` varchar(64) NOT NULL,
  `last_name` varchar(64) NOT NULL,
  `address` text NOT NULL,
  `email` varchar(64) NOT NULL,
  `mobile` varchar(12) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=15 ;

Connect to Database

Go to the config folder and open database.php file some changes in this file like hostname, database username, database password, and database name.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'enter here database name',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);

Create Controller

In this step, we will create a Ci_autocomplete.php file in the “application/controller” directory and paste the below code in this controller.
application/controller/Ci_autocomplete.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
 
 
class Ci_autocomplete extends CI_Controller {
 
 
    /**
    * Constructor of Controller
    *
    * @return Response
   */
    public function __construct() {
        parent::__construct();
        $this->load->database();
    }
 
    public function index()
    {
    $this->load->view('autocomplete');
    }
 
    public function search()
    {
        $query = $this->input->get('query');
        $this->db->like('first_name', $query);
        $data_result = $this->db->get("users")->result();
$data = array();
        foreach ($data_result as $row)
{
   $data[] = $row->first_name;
}
        echo json_encode($data);
    }
}
 
?>

Create View File

So finally, we will create the autocomplete.php file in the “application/views/” directory.
application/views/autocomplete.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<html lang="en">
<head>
    <title>How to Autocomplete Textbox in CodeIgniter using Typeahead  - XpertPhp</title>  
    <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.6/css/bootstrap.min.css" />
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.js"></script>
    <script src="//cdnjs.cloudflare.com/ajax/libs/bootstrap-3-typeahead/4.0.1/bootstrap3-typeahead.min.js"></script>  
</head>
<body>
<div class="container">
    <h2>Autocomplete Textbox in CodeIgniter using Typeahead </h2>
    <input class="typeahead form-control" type="text">
</div>
<script type="text/javascript">
    $('input.typeahead').typeahead({
        source:  function (query, process) {
        return $.get('<?php echo base_url(); ?>ci_autocomplete/search', { query: query }, function (data) {
                data = $.parseJSON(data);
                return process(data);
            });
        }
    });
</script>
</body>
</html>

We think would you like this article, so you can click on the “Show Demo” button and you can see this demo article.

Show Demo

Codeigniter, Ajax, Jquery, MySql Tags:autocomplete, autocomplete in codeigniter, autocomplete jquery ajax php, autocomplete jquery codeigniter, Autocomplete Seach, autocomplete typeahead, jquery autocomplete example, jquery autocomplete from database, typeahead in codeigniter

Post navigation

Previous Post: php crud operation using pdo extension
Next Post: CodeIgniter CRUD Operations with MySQL

Latest Posts

  • Laravel 12 Ajax CRUD Example
  • Laravel 12 CRUD Example Tutorial
  • How to Create Dummy Data in Laravel 11
  • Laravel 11 Yajra Datatables Example
  • Laravel 11 Ajax CRUD Example
  • Laravel 11 CRUD Example Tutorial
  • Laravel 10 Ajax CRUD Example Tutorial
  • Laravel 10 CRUD Example Tutorial
  • How to disable button in React js
  • JavaScript Interview Questions and Answers

Tools

  • Compound Interest Calculator
  • Hex to RGB Color Converter
  • Pinterest Video Downloader
  • Birthday Calculator
  • Convert JSON to PHP Array Online
  • JavaScript Minifier
  • CSS Beautifier
  • CSS Minifier
  • JSON Beautifier
  • JSON Minifier

Categories

  • Ajax
  • Angular
  • Angularjs
  • Bootstrap
  • Codeigniter
  • Css
  • Htaccess
  • Interview
  • Javascript
  • Jquery
  • Laravel
  • MongoDB
  • MySql
  • Nodejs
  • Php
  • React JS
  • Shopify Api
  • Ubuntu

Tags

angular 10 tutorial angular 11 ci tutorial codeigniter 4 image upload Codeigniter 4 Tutorial codeigniter tutorial CodeIgniter tutorial for beginners codeigniter with mysql crud operation eloquent relationships file upload File Validation form validation Image Upload jQuery Ajax Form Handling jquery tutorial laravel 6 Laravel 6 Eloquent Laravel 6 Model laravel 6 relationship laravel 6 relationship eloquent Laravel 6 Routing laravel 7 Laravel 7 Eloquent laravel 7 routing laravel 7 tutorial Laravel 8 laravel 8 example laravel 8 tutorial laravel 9 example laravel 9 tutorial Laravel Framework laravel from scratch laravel social login learn jquery nodejs pagination payment gateway php with mysql react js example react js tutorial send mail validation wysiwyg editor wysiwyg html editor

Copyright © 2018 - 2025,

All Rights Reserved Powered by XpertPhp.com