/
Temperature_Conversion.php
86 lines (75 loc) · 2.29 KB
/
Temperature_Conversion.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
<html>
<head>
<title>Temperature Conversion by Jian Liu</title>
</head>
<body>
<h1>Week 9 Lab Task 2</h1>
<form name="converter" method="get" action="Week9Lab_Task2_JianLiu.php">
<table>
<tr>
<th colspan="2">Temperature conversion</th>
</tr>
<tr>
<td>Degree:</td>
<td><input type="text" name="degree" id="degree"></td>
</tr>
<tr>
<td>Convert to:</td>
<td><input type="radio" name="type" id="type" value="cels">Celsius
<input type="radio" name="type" id="type" value="fahr">Fahrenheit</td>
</tr>
<tr>
<td colspan="2"><input type="submit" name="submitted" value="Convert!"><!--<input type="submit" id="btnConvert" value="Convert!"></td>-->
</tr>
</table>
</form>
<p style = 'font-weight: bold;'> Instruction: Please enter the base degree, and choose
the desired type of convision, then press the 'Convert!' button</p>
<?php
if (isset($_GET["submitted"])){
$degree = $type = "";
if (isset($_GET["degree"])){
$degree = $_GET["degree"];
}
if (isset($_GET["type"])){
$type = $_GET["type"];
}
if($degree != "" && $type != ""){
if (is_numeric($degree)){
converter($type, $degree);
}
else{
echo "<p style = 'color: red; font-size: 150%;'> Invalid input, please enter a number.</p>";
exit();
}
}
elseif ( $type == "" && $degree!= ""){
echo "<p style = 'color: red; font-size: 150%;'> Invalid input, please select the type of conversion.</p>";
exit();
}
elseif ($degree == "" && $type != "" ){
echo "<p style = 'color: red; font-size: 150%;'> Invalid input, please please enter the base degree.</p>";
exit();
}
elseif ($degree == "" && $type == ""){
echo "<p style = 'color: red; font-size: 150%;'> Invalid input, please enter the degree and select the type.</p>";
exit();
}
else{
exit();
}
}
else{}
function converter($type, $degree){
if ($type == "cels"){
$result = ($degree - 32) * 5/9;
echo "<p style = 'color: blue; font-size: 200%;'>The Fahrenheit you entered was $degree, convert to Celsius is $result</p>";
}
else{
$result = $degree * 9/5 + 32;
echo "<p style = 'color: blue; font-size: 200%;'>The Celsius you entered was $degree, convert to Fahrenheit is $result</p>";
}
}
?>
</body>
</html>