-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpromCalc.php
More file actions
114 lines (103 loc) · 4.41 KB
/
promCalc.php
File metadata and controls
114 lines (103 loc) · 4.41 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
<!DOCTYPE HTML>
<!--
Tip Calculator - Code Path Security Pre Work
-->
<html>
<head>
<title> Tip Calculator </title>
<meta charset="utf-8" />
</head>
<body style="width: 100%; height: 100%; background-color: green ">
<div style="top: 40%; left: 40%; position: absolute;">
<div id="main">
<?php
// define variables and set to empty values
$subTotalError = $tipError = "";
$split = 1;
$valid = true;
$subTotal = $tipPercentage = $tip = $total = 0;
if ($_SERVER["REQUEST_METHOD"] == "POST") {
//check if subtotal exist, if it exist, check if its valid
if (empty($_POST["subtotal"])) {
$subTotalError = "Subtotal is required";
$valid = false;
} else {
$subTotal = trim_input($_POST["subtotal"]);
//check if the input is a number
if (!preg_match("(^[0-9]*\.?[0-9]+$)", $subTotal)) {
$subTotalError = "Invalid Subtotal";
$subTotal = 0;
$valid = false;
} else {
//else round the subtotal to two decimal places
$subTotal = round(trim_input($_POST["subtotal"]), 2);
}
}
if (empty($_POST["tipPercent"])) {
$tipError = "Tip Percentage Required";
$valid = false;
} else {
//else round the subtotal to two decimal places
$tipPercentage = round(floatval($_POST["tipPercent"]), 2);
}
}
//used to trim the inputs before checking equality
function trim_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<h2 id="title">Tip Calculator</h2>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
Bill Subtotal: $<input type="text" name="subtotal" value="<?php echo $subTotal; ?>">
<br>
<span class="error"><?php echo $subTotalError; ?></span>
<br><br>
Tip Percent:
<br><br>
<?php
//if the tip percent is not currently empty, get the current percentage
if (!empty($_POST["tipPercent"]))
$prevTip = $_POST["tipPercent"];
else
$prevTip = 0.15; //set default value to be 15%
$tips = [10, 15, 20]; //array of tip values
//for each value of the array, output the radio button
foreach ($tips as &$tipPercent) {
printf("<input type=\"radio\" name=\"tipPercent\" value=\"%0.2f\" %s /> %d%% \t", $tipPercent / 100, (intval($prevTip * 100) == intval($tipPercent)) ? "checked" : "", $tipPercent);
}
print "<br>";
?>
<br><br>
<div align="center">
<input type="reset" class="button" value="Reset" onclick="reload_page()"/>
<input type="submit" class="button" name="submit" value="Submit">
</div>
<br><br>
</form>
<!-- use javascript to reset the page -->
<script>
function reload_page() {
window.location = "";
}
</script>
<?php
if ($valid && !empty($_POST["subtotal"])) {
echo "<div id=\"result\" style=\"border: 5px solid #aaa;\">";
echo "Tip: $";
$tip = $subTotal * $tipPercentage;
echo number_format($tip, 2);
echo "<br><br>";
echo "Total: $";
$total = $subTotal + $tip;
echo number_format($total, 2);
echo "<br><br>";
echo "</div>";
}
?>
</div>
</div>
</body>
</html>