-
Notifications
You must be signed in to change notification settings - Fork 1
/
checkout.php
367 lines (344 loc) · 14.9 KB
/
checkout.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
<?php
$servername = "localhost";
$username = "f31ee";
$password = "f31ee";
$dbname = "f31ee";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
echo "<script type='text/javascript'>alert('Connecting to database failed!');</script>";
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT * FROM `menu`";
if($result= mysqli_query($conn, $sql)){
#$num_rows = mysql_num_rows($result);
$num_rows = mysqli_num_rows($result); #for home use cos PHP 7
$menuID = array_fill(0, $num_rows, 0);
$foodname = array_fill(0, $num_rows, 0);
$price = array_fill(0, $num_rows, 0);
$i = 0;
while($row = mysqli_fetch_assoc($result)) {
#echo $row['timestamp'] . "<br>";
#echo $row['OrderID'] . "<br>";
$menuID[$i] = $row['menuID'];
$foodname[$i] = $row['foodname'];
$price[$i] = $row['price'];
$i++;
}
}
$box = array_fill(0, count($menuID), 0);
for($a=0;$a<count($menuID);$a++){
if($_POST['box' . $a] == '0'){
$box[$a] = $_POST['qtybox'. $a];
#echo $box[$a] . "<br>";
}
}
$sql = "SELECT orderID FROM `orders` ORDER BY OrderID DESC LIMIT 1";
if($result= mysqli_query($conn, $sql)){
while($row = mysqli_fetch_assoc($result)) {
$orderID = $row["orderID"] + 1;
}
}
#echo $orderID;
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Checkout Page</title>
<meta charset="utf-8">
<link rel="stylesheet" href="JHYJCSS.css">
<style>
#contenttitle {
margin-top: 30px;
margin-bottom: 50px;
text-align:center;
font-size: 200%;
color: #683e06;
border-bottom: 3px solid #683e06;
}
#desc {
font-size: 130%;
padding-bottom: 30px;
text-align: center;
color: #683e06;
}
#desc #coutlist {
margin: auto;
text-align: center;
color: #683e06;
border: 2px solid #683e06;
}
#coutlist th, #coutlist td, #coutlist tr {
padding: 5px;
font-size: 90%
}
.mid {
height: 1400px;
}
#custDet {
font-size: 70%;
color: #683e06;
width: 580px;
background-color: #e6dde4a6;
padding: 10px;
text-align: center;
margin: auto;
}
#custDet th, #custDet td {
padding: 10px;
}
body {
background-color: #bdc0c9;
background-image: url('background3.jpg');
background-size: auto;
background-repeat: no-repeat;
}
input[type="text"], input[type="email"] {
width: 200px;
height: 18px;
font-size: 80%;
}
#pay {
width: 70px;
height: 30px;
font-size: 80%;
color: #683e06;
border-radius: 5px;
margin-top: 5px;
}
#coutlist thead tr, #coutlist tfoot tr {
background-color: #d3b792d8
}
#coutlist tbody tr {
background-color: #dfccb3d8;
}
</style>
</head>
<body onload="reset()">
<script>
var name = document.getElementById("custname");
var add = document.getElementById("custaddress");
var email = document.getElementById("custemail");
var num = document.getElementById("custphone");
function reset() {//form reset
document.getElementById("myform").reset();
}
function validateName() { //validate name
var name = document.getElementById("custname");
var regexp = /^[A-Za-z\s]{1,}$/; //At least one alphabetical chracters
if(name.value.length > 0) {
if(regexp.test(name.value) == false) {
document.getElementsByClassName("message")[0].innerHTML = "Invalid Name!";
document.getElementById("custname").style.border = "2px solid #e0150e";
document.getElementsByClassName("message")[0].style.color = "#e0150e";
document.getElementById("pay").disabled = true;
return false;
} else {
document.getElementsByClassName("message")[0].innerHTML = "";
document.getElementById("custname").style.border = "2px solid green";
document.getElementById("pay").disabled = false;
return true;
}
} else {
document.getElementsByClassName("message")[0].innerHTML = "Only Alphabetical Characters";
document.getElementsByClassName("message")[0].style.color = "";
document.getElementById("custname").style.border = "";
}
}
function validateAdd() { //validate address
var add = document.getElementById("custaddress");
var regexp = /^([\w][\s]){1,}$/; //at least one word char/whitespace
if(add.value.length > 0) {
if(regexp.test(add.value)) {
document.getElementsByClassName("message")[1].innerHTML = "Invalid Address!";
document.getElementById("custaddress").style.border = "2px solid #e0150e";
document.getElementsByClassName("message")[1].style.color = "#e0150e";
document.getElementById("pay").disabled = true;
return false;
} else {
document.getElementsByClassName("message")[1].innerHTML = "";
document.getElementById("custaddress").style.border = "2px solid green";
document.getElementById("pay").disabled = false;
return true;
}
} else {
document.getElementsByClassName("message")[1].innerHTML = "";
document.getElementsByClassName("message")[1].style.color = "";
document.getElementById("custaddress").style.border = "";
}
}
function validateEmail() { //validate email
var email = document.getElementById("custemail");
var regexp = /^[\w.-]+@([A-Za-z]+\.){1,3}[A-Za-z]{2,3}$/; //word charcters before @ and at least 2 and max 4 extensions
//last extension must be 2-3 characters
if(email.value.length > 0) {
if(regexp.test(email.value) == false) {
document.getElementsByClassName("message")[2].innerHTML = "Invalid Email!";
document.getElementById("custemail").style.border = "2px solid #e0150e";
document.getElementsByClassName("message")[2].style.color = "#e0150e";
document.getElementById("pay").disabled = true;
return false;
} else {
document.getElementsByClassName("message")[2].innerHTML = "";
document.getElementById("custemail").style.border = "2px solid green";
document.getElementById("pay").disabled = false;
return true;
}
} else {
document.getElementsByClassName("message")[2].innerHTML = "@ Required";
document.getElementsByClassName("message")[2].style.color = "";
document.getElementById("custemail").style.border = "";
}
}
function validatePhone() { //validate phone no.
var num = document.getElementById("custphone");
var regexp = /^[0-9]{8}$/; //can only enter exactly 8 numbers
if(num.value.length > 0) {
if(regexp.test(num.value) == false) {
document.getElementsByClassName("message")[3].innerHTML = "Invalid Contact Number!";
document.getElementById("custphone").style.border = "2px solid #e0150e";
document.getElementsByClassName("message")[3].style.color = "#e0150e";
document.getElementById("pay").disabled = true;
return false;
} else {
document.getElementsByClassName("message")[3].innerHTML = "";
document.getElementById("custphone").style.border = "2px solid green";
document.getElementById("pay").disabled = false;
return true;
}
} else {
document.getElementsByClassName("message")[3].innerHTML = "Exactly 8 numbers";
document.getElementsByClassName("message")[3].style.color = "";
document.getElementById("custphone").style.border = "";
}
}
</script>
<div class="wrapper">
<div class="top">
<header>
<div id="navigation">
<a href="index.html">
<img src="logo2.png" alt="Home" id="logo" style="width:300px;height:150px;border:0;">
</a>
<div class="tablenav">
<table align="right" id="navbar">
<tr>
<td colspan="2"></td>
<td align="right"><a href="cart.php" id="cart"><img src="cart.png" alt="Cart" style="width: 50px; height: 50px; border:0;"></a></td>
</tr>
<tr>
<td><div class="dropmenu">
<b>MENU</b>
<div class="dropcontent">
<b><a href="classic.php">CLASSIC</a></b>
<b><a href="custom.php">CUSTOM</a></b>
</div>
</div></td>
<td><b><a href="track.html">TRACK ORDER</a></b></td>
<td><b><a href="about.html" id="about">ABOUT US</a></b></td>
</tr>
</table>
</div>
</div>
</header>
</div>
<div class="mid">
<div id="contenttitle">
<b><p>CHECKOUT</p></b>
</div>
<div id="desc">
<p><a href="cart.php" style="font-size: 70%; color: #683e06;">Back to Cart</a></p>
<form action="checkout_success.php" method="post" id="myform">
<table border="1" name="coutlist" id="coutlist">
<thead>
<tr>
<th>Item Description</th>
<th>Quantity</th>
<th>Price</th>
</tr>
</thead>
<tbody>
<?php
$total = 0;
$sqlall = [];
$sqlorders = [];
$deletecart = [];
$d_timestamp = date("Y-m-d H:i:s", time() + 86400);
echo '<p id="couttime" style="font-size: 80%; color: #683e06;"><b>Checkout Time:</b> <span id="time"> '.$d_timestamp.'</span></p>';
for ($i=0; $i<count($menuID); $i++){
$j = $i + 1;
if(!empty($box[$i])){
echo "<tr>";
echo "<td>" .$foodname[$i]. "</td>";
echo "<td>" .$box[$i]. "</td>";
echo "<td>$" .number_format($price[$i], 2)*$box[$i]. "</td>";
echo "</tr>";
$total = $total + number_format($price[$i], 2)*$box[$i];
$sqlall[] = "INSERT INTO `orderdetails` (orderID, menuID, size, shape, topping, layer1, layer2, layer3, quantity) VALUES (".$orderID.", ".$j.", 0,0,0,0,0,0, ".$box[$i]." );";
$deletecart[] = $i;
}
}
$sqlorders[] = "INSERT INTO `orders` (amount) VALUES (" . $total. ");";
$sqlorders[] = "INSERT INTO `delivery` (orderID, deliverytime) VALUES (" . $orderID . ", '".$d_timestamp."');";
echo "</tbody><tfoot>
<tr>
<td align='right' colspan='2'><b>Total Due:</b></td>
<td>$".$total."</td>
</tr>
</tfoot>";
?>
</table>
<br>
<table border="0" name="custDet" id="custDet">
<tr align='left'>
<td colspan='2' align='left' style="font-size: 120%; color: #683e06;"><b>Customer Credentials</b></td>
</tr>
<tr>
<td align='right'>Name:</td>
<td align="left"><input type="text" name="custname" id="custname" oninput="validateName()" required></td>
<td align="left" class="message" style="font-size: 70%;">Only Alphabetical Characters</td>
</tr>
<tr>
<td align='right'>Delivery Address:</td>
<td align="left"><input type="text" name="custaddress" id="custaddress" oninput="validateAdd()" required></td>
<td align='left' class="message" style="font-size: 70%;"></td>
</tr>
<tr>
<td align='right'>Email:</td>
<td align="left"><input type="email" name="custemail" id="custemail" oninput="validateEmail()" required></td>
<td align='left' class="message" style="font-size: 70%;">@ Required</td>
</tr>
<tr>
<td align='right'>Contact Number:</td>
<td align="left"><input type="text" name="custphone" id="custphone" oninput="validatePhone()" required></td>
<td align='left' class="message" style="font-size: 70%;">Exactly 8 Numbers</td>
</tr>
</table>
<input type="submit" id="pay" value="Pay!" disabled>
<input type='hidden' name='sqlall' value="<?php echo htmlentities(serialize($sqlall)); ?>" >
<input type='hidden' name='sqlorders' value="<?php echo htmlentities(serialize($sqlorders)); ?>" >
<input type='hidden' name='delete' value="<?php echo htmlentities(serialize($deletecart)); ?>" >
<input type='hidden' name='orderID' value="<?php echo htmlentities(serialize($orderID)); ?>" >
</form>
<p style="font-size: 60%; color: #683e06;"><i>All prices are in imaginary dollars.</i></p>
<?php
#echo 'hi '. htmlspecialchars($_SESSION['cart'][10]);
?>
</div>
</div>
</div>
<div class="bot">
<footer>
<hr>
<br>
<div id="tm">
<small><i><b>Copyright © 2019 JHYJCakes</b></i></small>
</div>
<div id="links"><b>
<a href="about.html#left">CONTACT US</a>
<a href=""></a>
</b></div>
</footer>
</div>
</body>
</html>