-
Notifications
You must be signed in to change notification settings - Fork 2
/
calculator.php
97 lines (88 loc) · 4.18 KB
/
calculator.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
<?php
// print_r($_POST);
if($_SERVER["REQUEST_METHOD"] == "POST") {
$number_1 = filter_var($_POST['number_1'], FILTER_VALIDATE_FLOAT);
$number_2 = filter_var($_POST['number_2'], FILTER_VALIDATE_FLOAT);
if ($_POST['number_1'] != '' && $_POST['number_2'] != '' && $_POST['calculation'] != '') {
$calculation = $_POST['calculation'];
$total = 0;
switch ($calculation) {
case 'Addition':
$total = $number_1 + $number_2;
break;
case 'Subtraction':
$total = $number_1 - $number_2;
break;
case 'Multiplication':
$total = $number_1 * $number_2;
break;
case 'Division':
if ($number_2 == 0) {
$total = "Undefined";
$error = "Devide by 0 is undefined!";
}
else {
$total = $number_1 / $number_2;
}
break;
default:
$error = "Something went wrong!";
}
}
else {
$error = "All fields are required!";
}
}
?>
<!doctype html>
<html lang="en">
<head>
<?php require_once "_header.php" ?>
<title>Calculator</title>
</head>
<body>
<?php require_once "_navbar.php" ?>
<div class="container">
<div class="row justify-content-center mt-5">
<div class="col-12 col-sm-6 align-self-center">
<div class="card">
<div class="card-body">
<h1 class="text-center">Calculator</h1>
<?php if($error!=''): ?>
<div class="alert alert-warning text-center">
<?php echo $error; ?>
</div>
<?php endif; ?>
<form method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
<div class="form-group">
<label for="number_1">Number 1</label>
<input type="number" class="form-control" name="number_1" value="<?php echo $number_1; ?>" required>
</div>
<div class="form-group">
<label for="number_2">Number 2</label>
<input type="number" class="form-control" name="number_2" value="<?php echo $number_2; ?>" required>
</div>
<div class="form-group">
<label for="calculation">Calculation</label>
<select class="custom-select" name="calculation" required>
<option selected>Please Select</option>
<option value="Addition" <?php echo ($calculation == 'Addition')? 'Selected' : ''; ?>>Addition (+)</option>
<option value="Subtraction" <?php echo ($calculation == 'Subtraction')? 'Selected' : ''; ?>>Subtraction (-)</option>
<option value="Multiplication" <?php echo ($calculation == 'Multiplication')? 'Selected' : ''; ?>>Multiplication (*)</option>
<option value="Division" <?php echo ($calculation == 'Division')? 'Selected' : ''; ?>>Division (/)</option>
</select>
</div>
<div class="form-group">
<label for="total">Total</label>
<input type="text" class="form-control" name="total" value="<?php echo $total; ?>" disabled>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
</div>
</div>
</div>
</div>
<?php include_once "_footer.php" ?>
</body>
</html>