|
| 1 | +<?php |
| 2 | + |
| 3 | + require_once('includes/temperature.php'); |
| 4 | + |
| 5 | + // Default empty variables |
| 6 | + $fromValue = ''; |
| 7 | + $fromUnit = ''; |
| 8 | + $toUnit = ''; |
| 9 | + $toValue = ''; |
| 10 | + |
| 11 | + /* Check conversion request on submit */ |
| 12 | + if ($_POST['submit']) { |
| 13 | + $fromValue = $_POST['fromValue']; |
| 14 | + $fromUnit = $_POST['fromUnit']; |
| 15 | + $toUnit = $_POST['toUnit']; |
| 16 | + |
| 17 | + $toValue = convertTemperature($fromValue, $fromUnit, $toUnit); |
| 18 | + } |
| 19 | + |
| 20 | + $tempUnits = array( |
| 21 | + "Celsius", |
| 22 | + "Fahrenheit", |
| 23 | + "Kelvin" |
| 24 | + ); |
| 25 | +?> |
| 26 | +<!DOCTYPE html> |
| 27 | +<html> |
| 28 | + <head> |
| 29 | + <meta charset="UTF-8"> |
| 30 | + <title>Convert Temperature</title> |
| 31 | + <link href="styles.css" rel="stylesheet" type="text/css"> |
| 32 | + </head> |
| 33 | + <body> |
| 34 | + |
| 35 | + <div id="main-content"> |
| 36 | + |
| 37 | + <h1>Convert Temperature</h1> |
| 38 | + |
| 39 | + <form action="" method="post"> |
| 40 | + |
| 41 | + <div class="entry"> |
| 42 | + <label>From:</label> |
| 43 | + <input type="text" name="fromValue" value="<?php { echo $fromValue;} ?>" /> |
| 44 | + <select name="fromUnit"> |
| 45 | + <?php |
| 46 | + foreach ($tempUnits as $unit) { |
| 47 | + echo "<option value=\"" . strtolower($unit) . "\""; |
| 48 | + if ($fromUnit == strtolower($unit)) { |
| 49 | + { |
| 50 | + echo " selected"; |
| 51 | + } |
| 52 | + } |
| 53 | + echo ">" . $unit . "</option>"; |
| 54 | + } |
| 55 | + ?> |
| 56 | + </select> |
| 57 | + </div> |
| 58 | + |
| 59 | + <div class="entry"> |
| 60 | + <label>To:</label> |
| 61 | + <input type="text" name="toValue" value="<?php { echo $toValue;} ?>" /> |
| 62 | + <select name="toUnit"> |
| 63 | + <?php |
| 64 | + foreach ($tempUnits as $unit) { |
| 65 | + echo "<option value=\"" . strtolower($unit) . "\""; |
| 66 | + if ($toUnit == strtolower($unit)) { |
| 67 | + { |
| 68 | + echo " selected"; |
| 69 | + } |
| 70 | + } |
| 71 | + echo ">" . $unit . "</option>"; |
| 72 | + } |
| 73 | + ?> |
| 74 | + </select> |
| 75 | + |
| 76 | + </div> |
| 77 | + |
| 78 | + <input type="submit" name="submit" value="Submit" /> |
| 79 | + </form> |
| 80 | + |
| 81 | + <br/> |
| 82 | + <a href="index.php">Return to menu</a> |
| 83 | + |
| 84 | + </div> |
| 85 | +</body> |
| 86 | +</html> |
0 commit comments