-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproduct.php
311 lines (228 loc) · 7.94 KB
/
product.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
<?php
//product.php
include('rms.php');
$object = new rms();
if(!$object->is_login())
{
header("location:".$object->base_url."");
}
if(!$object->is_master_user())
{
header("location:".$object->base_url."dashboard.php");
}
$object->query = "
SELECT category_name FROM product_category_table
WHERE category_status = 'Enable'
ORDER BY category_name ASC
";
$category_result = $object->get_result();
include('header.php');
?>
<!-- Page Heading -->
<h1 class="h3 mb-4 text-gray-800">Product Management</h1>
<!-- DataTales Example -->
<span id="message"></span>
<div class="card shadow mb-4">
<div class="card-header py-3">
<div class="row">
<div class="col">
<h6 class="m-0 font-weight-bold text-primary">Product List</h6>
</div>
<div class="col" align="right">
<button type="button" name="add_product" id="add_product" class="btn btn-success btn-circle btn-sm"><i class="fas fa-plus"></i></button>
</div>
</div>
</div>
<div class="card-body">
<div class="table-responsive">
<table class="table table-bordered" id="product_table" width="100%" cellspacing="0">
<thead>
<tr>
<th>Product Name</th>
<th>Product Price</th>
<th>Product Category</th>
<th>Status</th>
<th>Action</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
</div>
</div>
<?php
include('footer.php');
?>
<div id="productModal" class="modal fade">
<div class="modal-dialog">
<form method="post" id="product_form">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title" id="modal_title">Add Data</h4>
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<div class="modal-body">
<span id="form_message"></span>
<div class="form-group">
<label>Category</label>
<select name="category_name" id="category_name" class="form-control" required data-parsley-trigger="change">
<option value="">Select Category</option>
<?php
foreach($category_result as $category)
{
echo '<option value="'.$category["category_name"].'">'.$category["category_name"].'</option>';
}
?>
</select>
</div>
<div class="form-group">
<label>Product Name</label>
<input type="text" name="product_name" id="product_name" class="form-control" required data-parsley-pattern="/^[a-zA-Z0-9 \s]+$/" data-parsley-trigger="keyup" />
</div>
<div class="form-group">
<label>Product Price</label>
<input type="text" name="product_price" id="product_price" class="form-control" required data-parsley-pattern="^[0-9]*\.[0-9]{2}$" data-parsley-trigger="keyup" />
</div>
</div>
<div class="modal-footer">
<input type="hidden" name="hidden_id" id="hidden_id" />
<input type="hidden" name="action" id="action" value="Add" />
<input type="submit" name="submit" id="submit_button" class="btn btn-success" value="Add" />
<button type="button" class="btn btn-default" data-dismiss="modal">Close now</button>
</div>
</div>
</form>
</div>
</div>
<script>
$(document).ready(function(){
var dataTable = $('#product_table').DataTable({
"processing" : true,
"serverSide" : true,
"order" : [],
"ajax" : {
url:"product_action.php",
type:"POST",
data:{action:'fetch'}
},
"columnDefs":[
{
"targets":[4],
"orderable":false,
},
],
});
$('#add_product').click(function(){
$('#product_form')[0].reset();
$('#product_form').parsley().reset();
$('#modal_title').text('Add Data');
$('#action').val('Add');
$('#submit_button').val('Add');
$('#productModal').modal('show');
$('#form_message').html('');
});
$('#product_form').parsley();
$('#product_form').on('submit', function(event){
event.preventDefault();
if($('#product_form').parsley().isValid())
{
$.ajax({
url:"product_action.php",
method:"POST",
data:$(this).serialize(),
dataType:'json',
beforeSend:function()
{
$('#submit_button').attr('disabled', 'disabled');
$('#submit_button').val('wait...');
},
success:function(data)
{
$('#submit_button').attr('disabled', false);
if(data.error != '')
{
$('#form_message').html(data.error);
$('#submit_button').val('Add');
}
else
{
$('#productModal').modal('hide');
$('#message').html(data.success);
dataTable.ajax.reload();
setTimeout(function(){
$('#message').html('');
}, 5000);
}
}
})
}
});
$(document).on('click', '.edit_button', function(){
var product_id = $(this).data('id');
$('#product_form').parsley().reset();
$('#form_message').html('');
$.ajax({
url:"product_action.php",
method:"POST",
data:{product_id:product_id, action:'fetch_single'},
dataType:'JSON',
success:function(data)
{
$('#category_name').val(data.category_name);
$('#product_name').val(data.product_name);
$('#product_price').val(data.product_price);
$('#modal_title').text('Edit Data');
$('#action').val('Edit');
$('#submit_button').val('Edit');
$('#productModal').modal('show');
$('#hidden_id').val(product_id);
}
})
});
$(document).on('click', '.status_button', function(){
var id = $(this).data('id');
var status = $(this).data('status');
var next_status = 'Enable';
if(status == 'Enable')
{
next_status = 'Disable';
}
if(confirm("Are you sure you want to "+next_status+" it?"))
{
$.ajax({
url:"product_action.php",
method:"POST",
data:{id:id, action:'change_status', status:status, next_status:next_status},
success:function(data)
{
$('#message').html(data);
dataTable.ajax.reload();
setTimeout(function(){
$('#message').html('');
}, 5000);
}
})
}
});
$(document).on('click', '.delete_button', function(){
var id = $(this).data('id');
if(confirm("Are you sure you want to remove it?"))
{
$.ajax({
url:"product_action.php",
method:"POST",
data:{id:id, action:'delete'},
success:function(data)
{
$('#message').html(data);
dataTable.ajax.reload();
setTimeout(function(){
$('#message').html('');
}, 5000);
}
})
}
});
});
</script>