-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
36 lines (30 loc) · 1018 Bytes
/
index.php
File metadata and controls
36 lines (30 loc) · 1018 Bytes
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
<?php
require_once __DIR__ . '/data/lengths.php';
require_once __DIR__ . '/includes/functions.php';
$value = "";
$unitFrom = "";
$unitTo = "";
$result = null;
$pageTitle = "Length Converter";
$units = $lengths;
$currentPage = 'length';
$errorMessage = "";
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Retrieve the data from $POST
$value = filter_input(INPUT_POST, 'value', FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION);
$unitFrom = $_POST['unitFrom'] ?? '';
$unitTo = $_POST['unitTo'] ?? '';
// Check if they are numbers
if ($value < 0) {
$errorMessage = "The value cannot be negative for this category.";
} elseif ($value !== false && isset($units[$unitFrom]) && isset($units[$unitTo])) {
// Calculates the result
$result = convertUnits((float)$value, $unitFrom, $unitTo, $units);
}
}
include __DIR__ . '/templates/header.php';
?>
<main>
<h2><?php echo $pageTitle; ?></h2>
<?php include __DIR__ . '/templates/form.php'; ?>
</main>