119 lines
4.2 KiB
PHP
119 lines
4.2 KiB
PHP
<!DOCTYPE html>
|
|
<html lang="en">
|
|
|
|
<head>
|
|
<?php include_once "../../base/meta.php" ?>
|
|
|
|
<title>Calculator</title>
|
|
|
|
<script src="formValidation.js"></script>
|
|
</head>
|
|
|
|
<body>
|
|
<?php
|
|
include_once "../../base/settings.php";
|
|
include_once "../../base/headers.php";
|
|
Headers::html();
|
|
|
|
if (
|
|
isset($_POST["operand1"], $_POST["operand2"], $_POST["operator"]) &&
|
|
is_numeric($_POST["operand1"]) &&
|
|
is_numeric($_POST["operand2"])
|
|
) {
|
|
|
|
$operand1 = $_POST["operand1"];
|
|
$operand2 = $_POST["operand2"];
|
|
$operator = $_POST["operator"];
|
|
|
|
switch ($operator) {
|
|
case "addition":
|
|
$result = $operand1 + $operand2;
|
|
break;
|
|
case "subtraction":
|
|
$result = $operand1 - $operator2;
|
|
break;
|
|
case "multiplication":
|
|
$result = $operand1 * $operand2;
|
|
break;
|
|
case "division":
|
|
if ($operand2 == 0) {
|
|
$result = "Not defined";
|
|
break;
|
|
}
|
|
$result = $operand1 / $operand2;
|
|
break;
|
|
}
|
|
} else if (isset($_POST["firstLoad"])) {
|
|
$errors = [];
|
|
|
|
if (!isset($_POST["operand1"]))
|
|
array_push($errors, "Operand 1 not set");
|
|
else if (!is_numeric($_POST["operand1"]))
|
|
array_push($errors, "Operand 1 not numeric");
|
|
|
|
if (!isset($_POST["operand2"]))
|
|
array_push($errors, "Operand 2 not set");
|
|
else if (!is_numeric($_POST["operand2"]))
|
|
array_push($errors, "Operand 2 not numeric");
|
|
|
|
if (!isset($_POST["operator"]))
|
|
array_push($errors, "No operator chosen");
|
|
}
|
|
?>
|
|
|
|
<div class="form-check form-switch m-3">
|
|
<input class="form-check-input" type="checkbox" role="switch" id="clientSideValidate">
|
|
<label class="form-check-label" for="clientSideValidate">Use Client Side Validation</label>
|
|
</div>
|
|
<form method="post" class="p-3 needs-validation" novalidate>
|
|
<div class="mb-3">
|
|
<label class="form-label" for="operand1">Operand 1</label>
|
|
<input class="form-control" id="operand1" type="number" name="operand1" value="<?php echo isset($result) && is_numeric($result) ? $result : 0 ?>" required step="any" />
|
|
<div class="invalid-feedback">
|
|
Please enter a number
|
|
</div>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label class="form-label" for="operand2">Operand 2</label>
|
|
<input class="form-control" id="operand2" type="number" name="operand2" value="0" required step="any" />
|
|
<div class="invalid-feedback">
|
|
Please enter a number
|
|
</div>
|
|
</div>
|
|
|
|
<label class="form-label">Operator</label>
|
|
<div class="form-check">
|
|
<input class="form-check-input" id="additionRadio" type="radio" name="operator" value="addition" <?php echo array_key_exists("operator", $_POST) && $_POST['operator'] == 'addition' ? 'checked' : '' ?> required />
|
|
<label class="form-check-label" for="additionRadio">+</label>
|
|
</div>
|
|
<div class="form-check">
|
|
<input class="form-check-input" id="subtractionRadio" type="radio" name="operator" value="subtraction" <?php echo array_key_exists("operator", $_POST) && $_POST['operator'] == 'subtraction' ? 'checked' : '' ?> required />
|
|
<label class="form-check-label" for="subtractionRadio">-</label>
|
|
</div>
|
|
<div class="form-check">
|
|
<input class="form-check-input" id="multiplicationRadio" type="radio" name="operator" value="multiplication" <?php echo array_key_exists("operator", $_POST) && $_POST['operator'] == 'multiplication' ? 'checked' : '' ?> required />
|
|
<label class="form-check-label" for="multiplicationRadio">*</label>
|
|
</div>
|
|
<div class="form-check">
|
|
<input class="form-check-input" id="divisionRadio" type="radio" name="operator" value="division" <?php echo array_key_exists("operator", $_POST) && $_POST['operator'] == 'division' ? 'checked' : '' ?> required />
|
|
<label class="form-check-label" for="divisionRadio">/</label>
|
|
<div class="invalid-feedback">
|
|
Please choose an operator
|
|
</div>
|
|
</div>
|
|
<button class="btn btn-primary mt-3" name="firstLoad" value="0">Berechnen</button>
|
|
</form>
|
|
<div class="spacer-1"></div>
|
|
<?php if (isset($result)) { ?>
|
|
<h2 class="m-3">
|
|
Result: <?= $result ?>
|
|
</h2>
|
|
<?php } ?>
|
|
<?php if (isset($errors)) {
|
|
foreach ($errors as $error) { ?>
|
|
<h2 class="text-danger"><?= $error ?></h2>
|
|
<?php }
|
|
} ?>
|
|
</body>
|
|
|
|
</html>
|