-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
353 lines (312 loc) · 8.76 KB
/
main.js
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
347
348
349
350
351
352
353
//Saima Naz and Dalee Kumawat
$(document).ready(function(){
cat();
brand();
product();
//cat() is a funtion fetching category record from database whenever page is load
function cat(){
$.ajax({
url : "action.php",
method: "POST",
data : {category:1},
success : function(data){
$("#get_category").html(data);
}
})
}
//brand() is a funtion fetching brand record from database whenever page is load
function brand(){
$.ajax({
url : "action.php",
method: "POST",
data : {brand:1},
success : function(data){
$("#get_brand").html(data);
}
})
}
//product() is a funtion fetching product record from database whenever page is load
function product(){
$.ajax({
url : "action.php",
method: "POST",
data : {getProduct:1},
success : function(data){
$("#get_product").html(data);
}
})
}
/* when page is load successfully then there is a list of categories when user click on category we will get category id and
according to id we will show products
*/
$("body").delegate(".category","click",function(event){
$("#get_product").html("<h3>Loading...</h3>");
event.preventDefault();
var cid = $(this).attr('cid');
$.ajax({
url : "action.php",
method : "POST",
data : {get_seleted_Category:1,cat_id:cid},
success : function(data){
$("#get_product").html(data);
if($("body").width() < 480){
$("body").scrollTop(683);
}
}
})
})
/* when page is load successfully then there is a list of brands when user click on brand we will get brand id and
according to brand id we will show products
*/
$("body").delegate(".selectBrand","click",function(event){
event.preventDefault();
$("#get_product").html("<h3>Loading...</h3>");
var bid = $(this).attr('bid');
$.ajax({
url : "action.php",
method : "POST",
data : {selectBrand:1,brand_id:bid},
success : function(data){
$("#get_product").html(data);
if($("body").width() < 480){
$("body").scrollTop(683);
}
}
})
})
/*
At the top of page there is a search box with search button when user put name of product then we will take the user
given string and with the help of sql query we will match user given string to our database keywords column then matched product
we will show
*/
$("#search_btn").click(function(){
$("#get_product").html("<h3>Loading...</h3>");
var keyword = $("#search").val();
if(keyword != ""){
$.ajax({
url : "action.php",
method : "POST",
data : {search:1,keyword:keyword},
success : function(data){
$("#get_product").html(data);
if($("body").width() < 480){
$("body").scrollTop(683);
}
}
})
}
})
//end
/*
Here #login is login form id and this form is available in index.php page
from here input data is sent to login.php page
if you get login_success string from login.php page means user is logged in successfully and window.location is
used to redirect user from home page to profile.php page
*/
$("#login").on("submit",function(event){
event.preventDefault();
$(".overlay").show();
$.ajax({
url : "login.php",
method: "POST",
data :$("#login").serialize(),
success :function(data){
if(data == "login_success"){
window.location.href = "profile.php";
}else if(data == "cart_login"){
window.location.href = "cart.php";
}else{
$("#e_msg").html(data);
$(".overlay").hide();
}
}
})
})
//end
//Get User Information before checkout
$("#signup_form").on("submit",function(event){
event.preventDefault();
$(".overlay").show();
$.ajax({
url : "register.php",
method : "POST",
data : $("#signup_form").serialize(),
success : function(data){
$(".overlay").hide();
if (data == "register_success") {
window.location.href = "cart.php";
}else{
$("#signup_msg").html(data);
}
}
})
})
//Get User Information before checkout end here
//Add Product into Cart
$("body").delegate("#product","click",function(event){
var pid = $(this).attr("pid");
event.preventDefault();
$(".overlay").show();
$.ajax({
url : "action.php",
method : "POST",
data : {addToCart:1,proId:pid},
success : function(data){
count_item();
getCartItem();
$('#product_msg').html(data);
$('.overlay').hide();
}
})
})
//Add Product into Cart End Here
//Count user cart items funtion
count_item();
function count_item(){
$.ajax({
url : "action.php",
method : "POST",
data : {count_item:1},
success : function(data){
$(".badge").html(data);
}
})
}
//Count user cart items funtion end
//Fetch Cart item from Database to dropdown menu
getCartItem();
function getCartItem(){
$.ajax({
url : "action.php",
method : "POST",
data : {Common:1,getCartItem:1},
success : function(data){
$("#cart_product").html(data);
}
})
}
//Fetch Cart item from Database to dropdown menu
/*
Whenever user change qty we will immediate update their total amount by using keyup funtion
but whenever user put something(such as ?''"",.()''etc) other than number then we will make qty=1
if user put qty 0 or less than 0 then we will again make it 1 qty=1
('.total').each() this is loop funtion repeat for class .total and in every repetation we will perform sum operation of class .total value
and then show the result into class .net_total
*/
$("body").delegate(".qty","keyup",function(event){
event.preventDefault();
var row = $(this).parent().parent();
var price = row.find('.price').val();
var qty = row.find('.qty').val();
if (isNaN(qty)) {
qty = 1;
};
if (qty < 1) {
qty = 1;
};
var total = price * qty;
row.find('.total').val(total);
var net_total=0;
$('.total').each(function(){
net_total += ($(this).val()-0);
})
$('.net_total').html("Total : Rs " +net_total);
})
//Change Quantity end here
/*
whenever user click on .remove class we will take product id of that row
and send it to action.php to perform product removal operation
*/
$("body").delegate(".remove","click",function(event){
var remove = $(this).parent().parent().parent();
var remove_id = remove.find(".remove").attr("remove_id");
$.ajax({
url : "action.php",
method : "POST",
data : {removeItemFromCart:1,rid:remove_id},
success : function(data){
$("#cart_msg").html(data);
checkOutDetails();
}
})
})
/*
whenever user click on .update class we will take product id of that row
and send it to action.php to perform product qty updation operation
*/
$("body").delegate(".update","click",function(event){
var update = $(this).parent().parent().parent();
var update_id = update.find(".update").attr("update_id");
var qty = update.find(".qty").val();
$.ajax({
url : "action.php",
method : "POST",
data : {updateCartItem:1,update_id:update_id,qty:qty},
success : function(data){
$("#cart_msg").html(data);
checkOutDetails();
}
})
})
checkOutDetails();
net_total();
/*
checkOutDetails() function work for two purposes
First it will enable php isset($_POST["Common"]) in action.php page and inside that
there is two isset funtion which is isset($_POST["getCartItem"]) and another one is isset($_POST["checkOutDetials"])
getCartItem is used to show the cart item into dropdown menu
checkOutDetails is used to show cart item into Cart.php page
*/
function checkOutDetails(){
$('.overlay').show();
$.ajax({
url : "action.php",
method : "POST",
data : {Common:1,checkOutDetails:1},
success : function(data){
$('.overlay').hide();
$("#cart_checkout").html(data);
net_total();
}
})
}
/*
net_total function is used to calcuate total amount of cart item
*/
function net_total(){
var net_total = 0;
$('.qty').each(function(){
var row = $(this).parent().parent();
var price = row.find('.price').val();
var total = price * $(this).val()-0;
row.find('.total').val(total);
})
$('.total').each(function(){
net_total += ($(this).val()-0);
})
$('.net_total').html("Total : Rs. " +net_total);
}
//remove product from cart
page();
function page(){
$.ajax({
url : "action.php",
method : "POST",
data : {page:1},
success : function(data){
$("#pageno").html(data);
}
})
}
$("body").delegate("#page","click",function(){
var pn = $(this).attr("page");
$.ajax({
url : "action.php",
method : "POST",
data : {getProduct:1,setPage:1,pageNumber:pn},
success : function(data){
$("#get_product").html(data);
}
})
})
})