Codeigniter Autocomplete With Jquery UI
Codeigniter Autocomplete With Jquery UI
Codeigniter Autocomplete With Jquery UI
Why?
In this tutorial, you will learn all the things you need to know about
What is Autocomplete?
experience (UX).
If you googling on google and typing some keywords, then you will
If you search for your favorite videos on youtube, you will also find
Step 1. Preparation
This is important!
If you missed this step, it means you missed the whole of this tutorial.
The better your preparation, the more likely you will succeed in
1. Codeiginter
website: www.codeigniter.com
2. Bootstrap
Bootstrap is a framework to beautify the user interface (UI). If you do
website: www.getbootstrap.com
3. Jquery
This is important!
website: www.jquery.com
4. Jquery UI
website: http://jqueryui.com/
Step 2. Database preparation
In this tutorial, I use mysql as Database Management System
(DBMS).
But,
If you are using other DBMS like Oracle, SQL Server, Maria DB, or
MongoDB.
No, Problem!
named autocomplete_db.
3 blog_title VARCHAR(100),
4 blog_description TEXT
)ENGINE=INNODB;
5
Next, insert some data in the blog table by executing the following
query:
Like this:
create css and js folder inside the assets folder. After that, include
the bootstrap, jquery UI, and jquery files inside
1. Autoload.php
like this:
1 $autoload['libraries'] = array();
2 $autoload['helper'] = array();
1 $autoload['libraries'] = array('database');
2 $autoload['helper'] = array('url');
2. Config.php
To configure the config.php, please open the folder:
application/config/config.php
like this:
Open config.php file using text editor, and then find this code:
1 $config['base_url'] = '';
1 $config['base_url'] = 'http://localhost/autocomplete/';
3. Database.php
To configure the database.php, please open the folder:
application/config/database.php
like this:
Open database.php file using text editor, and then find this code:
1 $active_group = 'default';
$query_builder = TRUE;
2
3
$db['default'] = array(
4
'dsn' => '',
5
'hostname' => 'localhost',
6
'username' => '',
7
'password' => '',
8 'database' => '',
9 'dbdriver' => 'mysqli',
10 'dbprefix' => '',
23
24
$active_group = 'default';
1
$query_builder = TRUE;
2
3
$db['default'] = array(
4
'dsn' => '',
5
'hostname' => 'localhost',
6 'username' => 'root',
7 'password' => '',
22
23
24
Step 5. Controller
Go ahead and create a controller file controllers/Blog.php with the
1 <?php
function __construct(){
3
parent::__construct();
4
$this->load->model('blog_model');
5
}
6
7
function index(){
8
$this->load->view('blog_view');
9 }
10
11 function get_autocomplete(){
12 if (isset($_GET['term'])) {
13 $result = $this->blog_model->search_blog($_GET['term']);
14 if (count($result) > 0) {
21 }
22
23
Step 6. Model
Go ahead and create a model file models/Blog_model.php with the
1
<?php
2
class Blog_model extends CI_Model{
3
4 function search_blog($title){
5 $this->db->like('blog_title', $title , 'both');
6 $this->db->order_by('blog_title', 'ASC');
7 $this->db->limit(10);
return $this->db->get('blog')->result();
8
}
9
10
}
11
Step 7. View
Go ahead and create a view file views/blog_view.php with the
1 <!DOCTYPE html>
<html>
2
<head>
3
<meta charset="utf-8">
4
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fi
5
<title>Autocomplete</title>
6 <link rel="stylesheet" href="<?php echo base_url().'assets/css/bootstrap.css'?>"
7 <link rel="stylesheet" href="<?php echo base_url().'assets/css/jquery-ui.css'?>"
8 </head>
9 <body>
<div class="container">
10
<div class="row">
11
<h2>Autocomplete Codeigniter</h2>
12
</div>
13
<div class="row">
14
<form>
15 <div class="form-group">
16 <label>Title</label>
18 </div>
</form>
19
</div>
20
</div>
21
22
<script src="<?php echo base_url().'assets/js/jquery-3.3.1.js'?>" type="text/ja
23
<script src="<?php echo base_url().'assets/js/bootstrap.js'?>" type="text/javas
24
<script src="<?php echo base_url().'assets/js/jquery-ui.js'?>" type="text/javas
25 <script type="text/javascript">
26 $(document).ready(function(){
27 $( "#title" ).autocomplete({
32
</body>
33 </html>
34
35
36
37
Done.
http://localhost/autocomplete/index.php/blog/
Then it will appear form with input text. And then type a few keywords
from the blog title in the database, then you will see autocomplete
like this:
But Wait.!!!
This is important!!!
Autocomple Dropdown
Autocomplete dropdown is autocomplete where when one option of
Example:
Let’s begin.
1 function get_autocomplete(){
2 if (isset($_GET['term'])) {
$result = $this->blog_model->search_blog($_GET['term']);
3
if (count($result) > 0) {
4
foreach ($result as $row)
5
$arr_result[] = array(
6
'label' => $row->blog_title,
7
'description' => $row->blog_description,
8
);
9
echo json_encode($arr_result);
10 }
11 }
12 }
13
After that, open view blog_view.php, and then change to be like this:
1 <!DOCTYPE html>
2 <html>
<head>
3
<meta charset="utf-8">
4
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fi
5
<title>Autocomplete</title>
6
<link rel="stylesheet" href="<?php echo base_url().'assets/css/bootstrap.css'?>"
7 <link rel="stylesheet" href="<?php echo base_url().'assets/css/jquery-ui.css'?>"
8 </head>
9 <body>
10 <div class="container">
<div class="row">
11
<h2>Autocomplete Codeigniter</h2>
12
</div>
13
<div class="row">
14
<form>
15
<div class="form-group">
16 <label>Title</label>
17 <input type="text" name="title" class="form-control" id="title" pl
18 </div>
19 <div class="form-group">
<label>Description</label>
20
<textarea name="description" class="form-control" placeholder="D
21
</div>
22
</form>
23
</div>
24
</div>
25
29 <script type="text/javascript">
$(document).ready(function(){
30
31
$('#title').autocomplete({
32
source: "<?php echo site_url('blog/get_autocomplete');?>",
33
34
select: function (event, ui) {
35
$('[name="title"]').val(ui.item.label);
36 $('[name="description"]').val(ui.item.description);
37 }
38 });
39
40 });
41 </script>
42
</body>
43
</html>
44
45
46
47
48
Done,
testing.
when you select an option from autocomplete, then the search form
Let’s begin.
1 <?php
function __construct(){
3
parent::__construct();
4
$this->load->model('blog_model');
5
error_reporting(0);
6
}
7
8 function index(){
9 $this->load->view('tutorial_view');
10 }
11
12 function get_autocomplete(){
13 if (isset($_GET['term'])) {
$result = $this->blog_model->search_blog($_GET['term']);
14
if (count($result) > 0) {
15
foreach ($result as $row)
16
$arr_result[] = array(
17
'label' => $row->blog_title,
18 );
19 echo json_encode($arr_result);
20 }
21 }
22 }
23
function search(){
24
$title=$this->input->get('title');
25
$data['data']=$this->blog_model->search_blog($title);
26
27
$this->load->view('search_view',$data);
28
}
29
30 }
31
32
33
After that create more view files. Here we need two more views.
result (search_view.php).
1 <!DOCTYPE html>
2 <html>
3 <head>
<meta charset="utf-8">
4
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fi
5
<title>Autocomplete</title>
6
<link rel="stylesheet" href="<?php echo base_url().'assets/css/bootstrap.css'?>"
7
<link rel="stylesheet" href="<?php echo base_url().'assets/css/jquery-ui.css'?>"
8 </head>
9 <body>
10 <div class="container">
11 <div class="row">
12 <h2>Search</h2>
</div>
13
<div class="row">
14
<form id="form_search" action="<?php echo site_url('tutorial/search');?>
15
<div class="input-group">
16
<input type="text" name="title" class="form-control" id="title" pl
17 <span class="input-group-btn">
18 <button class="btn btn-info" type="submit">Search</button>
19 </span>
20 </div>
21 </form>
</div>
22
</div>
23
24
<script src="<?php echo base_url().'assets/js/jquery-3.3.1.js'?>" type="text/ja
25
<script src="<?php echo base_url().'assets/js/bootstrap.js'?>" type="text/javas
26
<script src="<?php echo base_url().'assets/js/jquery-ui.js'?>" type="text/javas
27 <script type="text/javascript">
28 $(document).ready(function(){
29
30 $('#title').autocomplete({
32
33 select: function (event, ui) {
34 $(this).val(ui.item.label);
$("#form_search").submit();
35
}
36
});
37
38
});
39
</script>
40
41 </body>
42 </html>
43
44
45
46
1 <!DOCTYPE html>
2 <html>
3 <head>
4 <meta charset="utf-8">
12 </div>
13 <div class="row">
14 <table class="table table-striped">
15 <thead>
<tr>
16
<th>Title</th>
17
<th>Description</th>
18
</tr>
19
</thead>
20 <tbody>
21 <?php foreach($data as $row):?>
22 <tr>
</tr>
25
<?php endforeach;?>
26
</tbody>
27
</table>
28
</div>
29 </div>
30
33
34 </body>
35 </html>
36
37
38
Done.
And then click one of the options autocomplete available, then the
form will be directly submitted without the need to click the search
button
Awesome right?
And then, you will see the result look like this:
Conclusion
In today's tutorial is how to create autocomplete with codeigniter and
jquery UI.
Experience (UX).
submit form.