/
task3.php
103 lines (85 loc) · 2.01 KB
/
task3.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
98
99
100
101
102
103
<?php
require_once 'functions.php';
$result = '';
$conversions = [
1 => 'F to C',
2 => 'C to F',
];
$validationErrors = [];
$operationFromPost = getValue($_POST, 'conversion');
$temperature = getValue($_POST, 'temperature');
function validateForm(&$errors) {
global $temperature, $conversion;
if (!validateRequired($temperature)) {
$errors['temperature'][] = 'Please enter the temperature.';
} else if (!validateNumeric($temperature)) {
$errors['temperature'][] = 'This must be a number!';
}
return empty($errors);
}
if ($_POST) {
validateForm($validationErrors);
}
switch($operationFromPost){
case 1:
$result = (5/9)*($temperature-32) . " C";
break;
case 2:
$result = ((9/5)*$temperature) + 32 . " F";
break;
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Temperature Converter</title>
<link rel="stylesheet" type="text/css" href="reset.css"/>
<style type="text/css">
form > div {
margin: 0 0 3px 0;
}
form > div > label {
display: inline-block;
width: 100px;
}
form div.error {
color: red;
}
form div.error input,
form div.error select,
form div.error textarea {
border-color: red;
}
</style>
</head>
<body>
<div id="main">
<form action="" method="post">
<div class="<?= getFieldErrorClass($validationErrors, 'temperature')?>">
<label for="temperature">Enter the temperature:</label>
<input type="text" id="temperature" name="temperature" value="<?= $temperature ?>"/>
<?= formErrors(getValue($validationErrors, 'temperature', [])) ?>
</div>
<br>
<br>
<div>
<label>Please choose the conversion type:</label>
<select name="conversion">
<?php foreach ($conversions as $key => $value) :?>
<option value="<?= $key?>"><?=$value?></option>
<?php endforeach;?>
</select>
<br>
<br>
<input type="submit" />
</div>
<div class="<?= getFieldTemperatureClass($validationErrors, 'temperature')?>">
<label for="result">The result is:</label>
<br>
<?= $result ?>
</div>
</form>
</div>
</body>
</html>