Repository navigation
Expand file tree
/
Copy pathpurchase.php
More file actions
333 lines (263 loc) · 11.3 KB
/
Copy pathpurchase.php
File metadata and controls
333 lines (263 loc) · 11.3 KB
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
<script>
//check if the user is logged in, otherwise redirect to the index page
//prevent user circumventing the log in
if (sessionStorage.getItem("user") === null) {
location.replace("index.html");
}
</script>
<?php
session_start();
require_once("dbcontroller.php");
$db_handle = new DBController();
if(!empty($_GET["action"])) {
switch($_GET["action"]) {
case "remove":
if(!empty($_SESSION["cart_item"])) {
//go through each item in the cart
foreach($_SESSION["cart_item"] as $k => $v) {
//if the item is found in the cart, then remove it from the session array
if($_GET["product_ID"] === $_SESSION["cart_item"][$k]['product_ID'])
unset($_SESSION["cart_item"][$k]);
//the the cart is empty then clear the session variable
if(empty($_SESSION["cart_item"]))
unset($_SESSION["cart_item"]);
}
}
break;
case "purchase":
//check if something in the cart
if(!empty($_SESSION["cart_item"])) {
//empty the cart
unset($_SESSION["cart_item"]);
//generate a random order number picking a number between 100000 and 1000000
$orderId = "A2021". rand(100000,1000000);
//redirect to the order summary page passing the order number and the total cost
header('Location: orderSummary.php?orderID='.$orderId . '&totalCost='.$_GET["totalCost"]);
}
break;
case "empty":
unset($_SESSION["cart_item"]); //empty the cart
break;
}
}
?>
<HTML>
<HEAD>
<TITLE>Purchase Toys</TITLE>
<!-- Bootstrap CSS -->
<link
href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta3/dist/css/bootstrap.min.css"
rel="stylesheet"
integrity="sha384-eOJMYsd53ii+scO/bJGFsiCZc+5NDVN2yr8+0RDqr0Ql0h+rP48ckxlpbzKgwra6"
crossorigin="anonymous"
/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="myStyle.css" type="text/css" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<style>
.bd-placeholder-img {
font-size: 1.125rem;
text-anchor: middle;
-webkit-user-select: none;
-moz-user-select: none;
user-select: none;
}
@media (min-width: 768px) {
.bd-placeholder-img-lg {
font-size: 3.5rem;
}
}
</style>
</HEAD>
<BODY>
<!-- Bootstrap navbar-->
<?php
include 'nav.php';
?>
<!-- END of Nav component -->
<!-- main body of the page, divided into two sections -->
<div class="container-fluid">
<div class = "row">
<div class="col-md-5 col-lg-7">
<div id="product-grid" >
<div class="txt-heading"><h5>Fidget Toy Purchase</h5></div>
<!--Purchase form adapted from https://getbootstrap.com/docs/4.0/examples/checkout/ -->
<h4 class="mb-3">Address</h4>
<?php
$totalCost = $_GET["totalCost"];
echo '<form class="needs-validation" novalidate method="POST" action="purchase.php?action=purchase&totalCost=' .$totalCost.'">';?>
<div class="row g-3">
<div class="col-sm-12">
<label for="firstName" class="form-label "><i class="fa fa-user"></i> Full name</label>
<input type="text" class="form-control form-control-sm" id="firstName" placeholder="" value="" required>
<div class="invalid-feedback">
Valid name is required.
</div>
</div>
<div class="col-12">
<label for="email" class="form-label"><i class="fa fa-envelope"></i> Email </label>
<input type="email" class="form-control form-control-sm" id="email" required>
<div class="invalid-feedback">
Please enter a valid email address for shipping updates.
</div>
</div>
<div class="col-12">
<label for="address" class="form-label"><i class="fa fa-address-card-o"></i> Address</label>
<input type="text" class="form-control form-control-sm" id="address" required>
<div class="invalid-feedback">
Please enter your shipping address.
</div>
</div>
<div class="col-md-5">
<label for="country" class="form-label"><i class="fa fa-institution"></i> City</label>
<input type="text" class="form-control form-control-sm" id="country" required>
<div class="invalid-feedback">
Please enter a city.
</div>
</div>
<div class="col-md-4">
<label for="state" class="form-label">State</label>
<input type="text" class="form-control form-control-sm" id="state" required>
<div class="invalid-feedback">
Please provide a valid state.
</div>
</div>
<div class="col-md-3">
<label for="zip" class="form-label">Zip</label>
<input type="text" class="form-control form-control-sm" id="zip" placeholder="" required>
<div class="invalid-feedback">
Zip code required.
</div>
</div>
</div>
<hr class="my-4">
<h4 class="mb-3">Payment</h4>
<div class="row gy-3">
<div class="col-md-6">
<label for="cc-name" class="form-label">Name on card</label>
<input type="text" class="form-control form-control-sm" id="cc-name" placeholder="" required>
<small class="text-muted">Full name as displayed on card</small>
<div class="invalid-feedback">
Name on card is required
</div>
</div>
<div class="col-md-6">
<label for="cc-number" class="form-label">Credit card number</label>
<input type="text" class="form-control form-control-sm" id="cc-number" placeholder="" required>
<div class="invalid-feedback">
Credit card number is required
</div>
</div>
<div class="col-md-3">
<label for="cc-expiration" class="form-label">Expiration Month</label>
<!-- <input type="text" class="form-control form-control-sm" id="cc-expiration" placeholder="" required> -->
<select required class="form-select form-control-sm" id="cc-expiration" placeholder="">
<option value=""></option>
<option value="01">January</option>
<option value="02">February </option>
<option value="03">March</option>
<option value="04">April</option>
<option value="05">May</option>
<option value="06">June</option>
<option value="07">July</option>
<option value="08">August</option>
<option value="09">September</option>
<option value="10">October</option>
<option value="11">November</option>
<option value="12">December</option>
</select>
<div class="invalid-feedback">
Expiration month required
</div>
</div>
<div class="col-md-3">
<label for="cc-cvv" class="form-label">Expiration Year</label>
<!-- <input type="text" class="form-control form-control-sm" id="cc-cvv" placeholder="" required> -->
<select class="form-select form-control-sm" id="cc-cvv" placeholder="" required>
<option value=""> </option>
<option value="16"> 2021</option>
<option value="17"> 2022</option>
<option value="18"> 2023</option>
<option value="19"> 2024</option>
</select>
<div class="invalid-feedback">
Expiration year required
</div>
</div>
<div class="col-md-2">
<label for="cc-cvv" class="form-label">CVV</label>
<input type="text" class="form-control form-control-sm" id="cc-cvv" maxlength="3" required>
<div class="invalid-feedback">
Security code required
</div>
</div>
<div class="col-md-2"></div>
<div class="col-md-4">
<label for="fname">Accepted Cards</label>
<div class="icon-container">
<i class="fa fa-cc-visa" style="color:navy;"></i>
<i class="fa fa-cc-amex" style="color:blue;"></i>
<i class="fa fa-cc-mastercard" style="color:red;"></i>
<i class="fa fa-cc-discover" style="color:orange;"></i>
</div>
</div>
</div>
</div>
<hr class="my-4">
<div class="row g-3" >
<div class="col-md-5">
<a role="button" class = "btn btn-secondary" href="shop.php">Cancel</a>
<button class=" btn btn-primary " id="payBtn" type="submit" value=" Pay " > Pay </button>
</div>
</form>
</div>
</div>
<!-- hold shopping basket to the right -->
<div class= "col-lg-5" >
<div id="shopping-cart" style = "border: 3px solid #e0e0e0;" style="float:right" >
<div class="txt-heading"><h5>Shopping Cart</h5></div>
<a role = "button" class="btn btn-secondary" id="btnEmpty" href="shop.php?action=empty">Empty Cart</a>
<?php
if(isset($_SESSION["cart_item"])){
$total_quantity = 0;
$total_price = 0;
include 'shoppingBasket.php';
?>
<script>
// show the total amt due in the Pay button
buttonTxt = document.getElementsByTagName("button")[2].value;
var totalCost = '<?php echo "€".number_format($total_price, 2); ?>';
buttonTxt = buttonTxt + " "+ totalCost;
document.getElementsByTagName("button")[2].innerHTML = buttonTxt;
</script>
<?php
} else {
?>
<div class="no-records">Your Cart is Empty</div>
<?php
}
?>
</div>
</div>
</div>
</div>
<!-- page footer -->
<?php
include 'footer.php';
?>
<!-- END of footer component -->
<script>
//set nav bar colour to the user choice
document.getElementsByTagName("nav")[0].style.backgroundColor = sessionStorage.navColour;
//set the greeting
document.getElementsByTagName("p")[0].innerHTML = "Hello " + sessionStorage.user + "!";
//when the user logs out; unset the user session variable and redirect to the index page
function logOut(){
sessionStorage.removeItem("user");
location.replace("index.html");
}
</script>
<script src="form-validation.js"></script>
</BODY>
</HTML>