-
Notifications
You must be signed in to change notification settings - Fork 0
/
tippy.php
83 lines (82 loc) · 1.92 KB
/
tippy.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
<!DOCTYPE html>
<html>
<head>
<title>Tip Calculator W</title>
</head>
<body>
<b>Tip Calculator W</b>
<?php
$bill = $_GET["bill"];
$percentage = $_GET["percentage"];
$custom = $_GET["custom"];
$split = $_GET["split"];
if (!$split) {
$split = 1;
}
?>
<form action="tippy.php">
<p>Bill subtotal: $
<?php
echo "<input type=\"text\" id=\"bill\" name=\"bill\" required=\"required\" value=$bill>";
?>
</p>
<p>Tip percentage: </p>
<p>
<?php
for($i = 10; $i <= 20; $i += 5) {
$value = $percentage * 100;
if ($i == $value) {
echo "<input type=\"radio\" name=\"percentage\" value=0.$i required checked> $i%";
}
else {
echo "<input type=\"radio\" name=\"percentage\" value=0.$i required> $i%";
}
}
if ($percentage == "on") {
echo "<input type=\"radio\" name=\"percentage\" required checked>";
}
else {
echo "<input type=\"radio\" name=\"percentage\" required>";
}
echo "Other: <input type=\"text\" name=\"custom\" value=$custom>%";
?>
</p>
<p>
<?php
echo "Split: <input type=\"text\" name=\"split\" value=$split required> persons";
?>
</p>
<p><input type="submit"></p>
</form>
<?php
if ($bill != "") {
if ($percentage == "on") {
$percentage = $custom / 100;
}
if (!is_numeric($bill) || $bill <= 0) {
echo "<p>Invalid bill</p>";
}
else if ($percentage == "") {
echo "<p>Missing percentage</p>";
}
else if (!is_numeric($percentage) || $percentage <= 0) {
echo "<p>Invalid percentage</p>";
}
else if ($split == "") {
echo "Missing split";
}
else if (!is_numeric($split) || $split <= 0) {
echo "<p>Invalid persons</p>";
}
else {
$tip = $bill * $percentage;
$splitTip = $tip / $split;
$total = $bill + $tip;
$splitTotal = $total / $split;
echo "<p>Tip: $$tip; Split Tip: $$splitTip</p>";
echo "<p>Total: $$total; Split Total: $$splitTotal</p>";
}
}
?>
</body>
</html>