-
Notifications
You must be signed in to change notification settings - Fork 0
/
welcome_message.php
346 lines (305 loc) · 15.2 KB
/
welcome_message.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>CodeIgniter Framework with AJAX</title>
<link href="<?php echo base_url('assests/bootstrap/css/bootstrap.min.css') ?>" rel="stylesheet">
<link href="<?php echo base_url('assests/datatables/css/dataTables.bootstrap.css') ?>" rel="stylesheet">
</head>
<body>
<div class="container" id="tblreload">
<center>
<h1>CodeIgniter Framework with AJAX Wama Softwere Practical Task</h1>
</center>
<h3>Employee Detail</h3>
<br />
<button class="btn btn-danger" id="del_all" style="float: right;">Bulk Delete Delete</button>
<button class="btn btn-success" onclick="add_employee()" style="float: right;"><i class="glyphicon glyphicon-plus"></i> Add New Employee</button>
<br />
<br />
<table id="table_id" class="table table-striped table-bordered" cellspacing="0" width="100%">
<thead>
<tr>
<th>Sr No</th>
<th>Select</th>
<th>Name</th>
<th>Contact No</th>
<th>Hobby</th>
<th>Category</th>
<th>Profile Pic</th>
<th style="width:125px;">Action
</th>
</tr>
</thead>
<tbody>
<?php
$i = 0;
foreach ($employees as $employee) {
$i++;
?>
<tr id="<?php echo $employee['id']; ?>">
<td><?php echo $i; ?></td>
<td><input class="checkbox1" id="checkbox[]" type="checkbox" name="ckbdelete[]" value="<?php echo $employee['id']; ?>"></td>
<td>
<span class="editSpan nme"><?php echo $employee['name']; ?></span>
<input class="editInput nme form-control input-sm" type="text" name="name" value="<?php echo $employee['name']; ?>" style="display: none;">
</td>
<td>
<span class="editSpan cnct"><?php echo $employee['contact_number']; ?></span>
<input class="editInput cnct form-control input-sm" type="text" name="contact" value="<?php echo $employee['contact_number']; ?>" style="display: none;">
</td>
<td>
<span class="editSpan hby"><?php echo $employee['hobby']; ?></span>
<input class="editInput hby form-control input-sm" type="text" name="hobby" value="<?php echo $employee['hobby']; ?>" style="display: none;">
</td>
<td>
<span class="editSpan cat catspan"><?php echo $employee['category_name']; ?></span>
<select name="category" class="form-control editInput cat" id="cate" style="display: none;">
<option value="">Select Category</option>
<?php foreach ($categorys as $category) { ?>
<option value="<?php echo $category['cat_id']; ?>">
<?php echo $category['category_name']; ?></option>
<?php } ?>
</select>
<input type="hidden" class="editInput" name="cat_id" value="<?php echo $employee['category_id'] ?>" >
</td>
<td>
<img class="editSpan img" src="<?php echo base_url('uploads/profile/') . $employee['profile_picture']; ?>" width="140px" height="90px">
<form id="form">
<input type="file" name="image" id="imgs" class="editInput img" style="display: none;">
</form>
</td>
<td>
<button class="btn btn-warning editBtn" style="float: none;"><i class="glyphicon glyphicon-pencil"></i></button>
<button class="btn btn-danger deleteBtn" style="float: none;" onclick="delete_employee(<?php echo $employee['id']; ?>)"><i class="glyphicon glyphicon-remove"></i></button>
<button type="button" class="btn btn-sm btn-success saveBtn" style="float: none; display: none;">Save</button>
</td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
<!-- Bootstrap modal -->
<div class="modal fade" id="modal_form" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h3 class="modal-title">Add New Employee</h3>
</div>
<div class="modal-body form">
<form action="#" id="form" class="form-horizontal">
<div class="form-body">
<div class="form-group">
<label class="control-label col-md-3">Name</label>
<div class="col-md-9">
<input name="name" class="form-control" id="emp_name" type="text" required>
<b class="text-danger" id="error_name"></b>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3">Contact No</label>
<div class="col-md-9">
<input name="contact_number" class="form-control" id="emp_contact" type="text" required>
<b class="text-danger" id="error_contact"></b>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3">Hobby</label>
<div class="col-md-9">
<input name="hobby" class="form-control" id="emp_hobby" type="text" required>
<b class="text-danger" id="error_hobby"></b>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3">Category</label>
<div class="col-md-9">
<select name="category" class="form-control" id="catgry" required>
<option value="">Select Category</option>
<?php foreach ($categorys as $category) { ?>
<option value="<?php echo $category['cat_id']; ?>">
<?php echo $category['category_name']; ?></option>
<?php } ?>
</select>
<b class="text-danger" id="error_cat"></b>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3">Profile Pic</label>
<div class="col-md-9">
<input type="file" id="file" name="file" required/>
<b class="text-danger" id="error_img"></b>
</div>
</div>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" id="btnSave" onclick="save()" class="btn btn-primary">Save</button>
<button type="button" class="btn btn-danger" data-dismiss="modal">Cancel</button>
</div>
</div>
</div>
</div>
<script src="<?php echo base_url('assests/jquery/jquery-3.1.0.min.js') ?>"></script>
<script src="<?php echo base_url('assests/bootstrap/js/bootstrap.min.js') ?>"></script>
<script src="<?php echo base_url('assests/datatables/js/jquery.dataTables.min.js') ?>"></script>
<script src="<?php echo base_url('assests/datatables/js/dataTables.bootstrap.js') ?>"></script>
<script>
$(document).ready(function () {
$('.editBtn').on('click', function () {
//hide edit span
$(this).closest("tr").find(".editSpan").hide();
//show edit input
$(this).closest("tr").find(".editInput").show();
//hide edit button
$(this).closest("tr").find(".editBtn").hide();
//show edit button
$(this).closest("tr").find(".saveBtn").show();
$(this).closest("tr").find(".deleteBtn").hide();
});
$('.saveBtn').on('click', function () {
var trObj = $(this).closest("tr");
var ID = $(this).closest("tr").attr('id');
var inputData = $(this).closest("tr").find(".editInput").serialize();
var form_data = new FormData();
var file_data = $('#imgs').prop('files')[0];
form_data.append('file', file_data);
$.ajax({
type: 'POST',
url: '<?php echo site_url('welcome/update_employee') ?>',
dataType: "json",
data: 'id=' + ID + '&' + inputData + '&' + form_data,
success: function (response) {
if (response.status == 'ok') {
trObj.find(".editSpan.nme").text(response.data.name);
trObj.find(".editSpan.cnct").text(response.data.contact_number);
trObj.find(".editSpan.hby").text(response.data.hobby);
trObj.find(".editSpan.cat").text(response.category_name);
trObj.find(".editInput").hide();
trObj.find(".saveBtn").hide();
trObj.find(".editSpan").show();
trObj.find(".editBtn").show();
trObj.find(".deleteBtn").show();
} else {
alert(response.msg);
}
}
});
});
});
</script>
<script type="text/javascript">
$(document).ready(function () {
$('#table_id').DataTable();
});
var save_method;
var table;
function add_employee()
{
save_method = 'add';
$('#form')[0].reset();
$('#modal_form').modal('show');
$('.modal-title').text('Add New Employee');
}
function save()
{
var url;
if (save_method == 'add')
{
url = "<?php echo site_url('welcome/add_employee') ?>";
}
var inpObj_name = document.getElementById("emp_name");
var inpObj_contact = document.getElementById("emp_contact");
var inpObj_hobby = document.getElementById("emp_hobby");
var inpObj_cate = document.getElementById("catgry");
var inpObj_img = document.getElementById("file");
if (inpObj_name.checkValidity() === false) {
document.getElementById("error_name").innerHTML = inpObj_name.validationMessage;
} else if (inpObj_contact.checkValidity() === false) {
document.getElementById("error_contact").innerHTML = inpObj_contact.validationMessage;
} else if (inpObj_hobby.checkValidity() === false) {
document.getElementById("error_hobby").innerHTML = inpObj_hobby.validationMessage;
} else if (inpObj_cate.checkValidity() === false) {
document.getElementById("error_cat").innerHTML = inpObj_cate.validationMessage;
} else if (inpObj_img.checkValidity() === false) {
document.getElementById("error_img").innerHTML = inpObj_img.validationMessage;
} else {
var name = $('#emp_name').val();
var contact = $('#emp_contact').val();
var hobby = $('#emp_hobby').val();
var category = $('#catgry').val();
var form_data = new FormData();
var file_data = $('#file').prop('files')[0];
form_data.append('file', file_data);
form_data.append('name', name);
form_data.append('contact', contact);
form_data.append('hobby', hobby);
form_data.append('category', category);
// ajax adding data to database
$.ajax({
// url: url,
type: "POST",
data: form_data,
dataType: "json",
contentType: false,
processData: false,
cache: false,
success: function (data)
{
//if success close modal and reload ajax table
$('#modal_form').modal('hide');
$('#table_id').load(' #table_id');
// location.reload();// for reload a page
},
error: function (jqXHR, textStatus, errorThrown)
{
alert('Error adding / update data');
}
});
}
}
function delete_employee(id)
{
if (confirm('Are you sure delete this employee?'))
{
// ajax delete data from database
$.ajax({
url: "<?php echo site_url('welcome/delete_employee/'); ?>/" + id,
type: "POST",
dataType: "JSON",
cache: false,
success: function (data)
{
$('#table_id').load(' #table_id');
},
error: function (jqXHR, textStatus, errorThrown)
{
alert('Error deleting data');
}
});
return false;
}
}
$("#del_all").on('click', function(e) {
e.preventDefault();
var checkValues = $('.checkbox1:checked').map(function()
{
return $(this).val();
}).get();
console.log(checkValues);
$.each( checkValues, function( i, val ) {
$("#"+val).remove();
});
$.ajax({
url: "<?php echo site_url('welcome/delete_checked_employee') ?>",
type: 'post',
data: 'ids=' + checkValues
}).done(function() {
});
});
</script>
</body>
</html>