Professional Documents
Culture Documents
CRUD Without Reload Page Using Ajax and Codeigniter
CRUD Without Reload Page Using Ajax and Codeigniter
Good News:
Well, It turns out, you can easily create CRUD application without
Right Now.!
This is the way I used a few months ago for my own project.
Today, in this tutorial, I will explain to you clearly how to make CRUD
STEP-BY-STEP!
What is AJAX?
AJAX (Asynchronous Javascript And XML) is a method or technique
With Ajax, Web applications can send and retrieve data from a server
form.
Awesome right?
several pages.
There are page to display data from database, create form page, and
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. Codeigniter
www.codeigniter.com
2. Bootstrap
Bootstrap is a framework to beautify the user interface (UI). If you do
www.getbootstrap.com
3. Jquery
This is important!
website:
www.jquery.com
4. Datatables
and has integrated with filters, show per page, and pagination.
link: https://datatables.net/download/index.
(DBMS).
But,
If you are using other DBMS like Oracle, SQL Server, Maria DB, or
MongoDB.
No, Problem!
name db_crud.
After that,
3 product_name VARCHAR(100),
4 product_price INT(11)
)ENGINE=INNODB;
5
Like this:
Open crud_ajax folder and create assets folder. And then include
the bootstrap, datatables, and jquery files inside the assets folder.
1. Autoload.php
application/config/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
like this:
Open config.php file using text editor, and then find this code:
1 $config['base_url'] = '';
1 $config['base_url'] = 'http://localhost/crud_ajax/';
3. Database.php
application/config/database.php
like this:
Open database.php file using text editor, and then find this code:
1 $active_group = 'default';
2 $query_builder = TRUE;
4 $db['default'] = array(
22 );
23
24
$active_group = 'default';
1
$query_builder = TRUE;
2
3
$db['default'] = array(
4
'dsn' => '',
5
'hostname' => 'localhost',
6
'username' => 'root',
7
'password' => '',
8 'database' => 'db_crud',
9 'dbdriver' => 'mysqli',
23
24
Step 5. Controller
Go ahead and create a controller file controllers/Product.php with
1 <?php
2 class Product extends CI_Controller{
3 function __construct(){
4 parent::__construct();
$this->load->model('product_model');
5
}
6
function index(){
7
$this->load->view('product_view');
8
}
9
10
function product_data(){
11 $data=$this->product_model->product_list();
12 echo json_encode($data);
13 }
14
15 function save(){
$data=$this->product_model->save_product();
16
17 echo json_encode($data);
18 }
19
function update(){
20
$data=$this->product_model->update_product();
21
echo json_encode($data);
22
}
23
24
function delete(){
25 $data=$this->product_model->delete_product();
26 echo json_encode($data);
27 }
28
29 }
30
31
Step 6. Model
Go ahead and create a model file models/Product_model.php with
1 <?php
2 class Product_model extends CI_Model{
3
4 function product_list(){
5 $hasil=$this->db->get('product');
6 return $hasil->result();
}
7
8
9 function save_product(){
10 $data = array(
18 function update_product(){
19 $product_code=$this->input->post('product_code');
20 $product_name=$this->input->post('product_name');
21 $product_price=$this->input->post('price');
22
23 $this->db->set('product_name', $product_name);
$this->db->set('product_price', $product_price);
24
$this->db->where('product_code', $product_code);
25
$result=$this->db->update('product');
26
return $result;
27
}
28
29 function delete_product(){
30 $product_code=$this->input->post('product_code');
31 $this->db->where('product_code', $product_code);
32 $result=$this->db->delete('product');
33 return $result;
}
34
35
}
36
37
38
Step 7. View
Go ahead and create a view file views/product_view.php with the
1 <!DOCTYPE html>
2 <html>
<head>
3
<meta charset="utf-8">
4
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=
5
<title>Product List</title>
6
<link rel="stylesheet" type="text/css" href="<?php echo base_url().'assets/css/boot
7
<link rel="stylesheet" type="text/css" href="<?php echo base_url().'assets/css/jque
8 <link rel="stylesheet" type="text/css" href="<?php echo base_url().'assets/css/data
9 </head>
10 <body>
11 <div class="container">
<!-- Page Heading -->
12
<div class="row">
13
<div class="col-12">
14
<div class="col-md-12">
15
<h1>Product
16 <small>List</small>
17 <div class="float-right"><a href="javascript:void(0);" class="btn b
New</a></div>
18
</h1>
19
</div>
20
21
<table class="table table-striped" id="mydata">
22
<thead>
23 <tr>
24 <th>Product Code</th>
25 <th>Product Name</th>
26 <th>Price</th>
</tr>
28
</thead>
29
<tbody id="show_data">
30
31
</tbody>
32
</table>
33 </div>
34 </div>
35
36 </div>
37
39 <form>
47 </div>
48 <div class="modal-body">
56 <div class="col-md-10">
57 <input type="text" name="product_name" id="product_name"
58 </div>
</div>
59
<div class="form-group row">
60
<label class="col-md-2 col-form-label">Price</label>
61
<div class="col-md-10">
62
<input type="text" name="price" id="price" class="form-co
63 </div>
64 </div>
65 </div>
66 <div class="modal-footer">
74
76 <form>
84 </div>
85 <div class="modal-body">
</div>
90
</div>
91
<div class="form-group row">
92
<label class="col-md-2 col-form-label">Product Name</labe
93
<div class="col-md-10">
94 <input type="text" name="product_name_edit" id="product_n
95 </div>
96 </div>
<div class="col-md-10">
99
<input type="text" name="price_edit" id="price_edit" clas
100
</div>
101
</div>
102
</div>
103 <div class="modal-footer">
104 <button type="button" class="btn btn-secondary" data-dismiss="modal
106 </div>
107 </div>
</div>
108
</div>
109
</form>
110
<!--END MODAL EDIT-->
111
112
<!--MODAL DELETE-->
113 <form>
114 <div class="modal fade" id="Modal_Delete" tabindex="-1" role="dialog" aria-l
</button>
121
</div>
122
<div class="modal-body">
123
<strong>Are you sure to delete this record?</strong>
124
</div>
125 <div class="modal-footer">
126 <input type="hidden" name="product_code_delete" id="product_code_de
129 </div>
</div>
130
</div>
131
</div>
132
</form>
133
<!--END MODAL DELETE-->
134
139
140<script type="text/javascript">
$(document).ready(function(){
141
show_product(); //call function show all product
142
143
$('#mydata').dataTable();
144
145
//function show all product
146
function show_product(){
147 $.ajax({
148 type : 'ajax',
success : function(data){
152
var html = '';
153
var i;
154
for(i=0; i<data.length; i++){
155
html += '<tr>'+
156 '<td>'+data[i].product_code+'</td>'+
157 '<td>'+data[i].product_name+'</td>'+
158 '<td>'+data[i].product_price+'</td>'+
164 }
165 $('#show_data').html(html);
}
166
167
});
168
}
169
170
//Save product
171
$('#btn_save').on('click',function(){
172
var product_code = $('#product_code').val();
173 var product_name = $('#product_name').val();
174 var price = $('#price').val();
175 $.ajax({
182 $('[name="product_name"]').val("");
$('[name="price"]').val("");
183
$('#Modal_Add').modal('hide');
184
show_product();
185
}
186
});
187 return false;
188 });
189
191 $('#show_data').on('click','.item_edit',function(){
195
$('#Modal_Edit').modal('show');
196
$('[name="product_code_edit"]').val(product_code);
197
$('[name="product_name_edit"]').val(product_name);
198 $('[name="price_edit"]').val(price);
199 });
200
202 $('#btn_update').on('click',function(){
211 $('[name="product_code_edit"]').val("");
212 $('[name="product_name_edit"]').val("");
213 $('[name="price_edit"]').val("");
$('#Modal_Edit').modal('hide');
214
show_product();
215
}
216
});
217
return false;
218 });
219
223
224 $('#Modal_Delete').modal('show');
$('[name="product_code_delete"]').val(product_code);
225
});
226
227
//delete record to database
228
$('#btn_delete').on('click',function(){
229
var product_code = $('#product_code_delete').val();
230 $.ajax({
231 type : "POST",
success: function(data){
235
$('[name="product_code_delete"]').val("");
236
$('#Modal_Delete').modal('hide');
237
show_product();
238
}
239 });
240 return false;
241 });
242
243 });
244
245</script>
</body>
246
</html>
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
Please click add new button to add new product, click edit button to
Congratulations!
You did it. Now, you can create a crud application without reloading
If you feel this tutorial is useful for you. Please share! Perhaps, this