|
14 | 14 | } |
15 | 15 |
|
16 | 16 | $categories = ConvertUOM::getConversionCategories(); |
| 17 | +$defaultCategory = $_POST['category'] ?? $categories[0]; |
17 | 18 | $units = []; |
18 | 19 | foreach ($categories as $category) { |
19 | 20 | $categoryUnits = ConvertUOM::getConversionCategoryUnitDetails($category)[$category]; |
|
48 | 49 | <label for="fromUnit" class="col-sm-2 col-form-label">From Unit</label> |
49 | 50 | <div class="col-sm-10"> |
50 | 51 | <select name="fromUnit" class="form-select"> |
51 | | - <?php foreach ($units[$_POST['category']] as $fromUnitCode => $fromUnitName) { |
| 52 | + <?php foreach ($units[$defaultCategory] as $fromUnitCode => $fromUnitName) { |
52 | 53 | echo "<option value=\"{$fromUnitCode}\" " . ((isset($_POST['fromUnit']) && $_POST['fromUnit'] === $fromUnitCode) ? 'selected' : '') . ">{$fromUnitName}</option>", PHP_EOL; |
53 | 54 | } ?> |
54 | 55 | </select> |
|
58 | 59 | <label for="toUnit" class="col-sm-2 col-form-label">To Unit</label> |
59 | 60 | <div class="col-sm-10"> |
60 | 61 | <select name="toUnit" class="form-select"> |
61 | | - <?php foreach ($units[$_POST['category']] as $toUnitCode => $toUnitName) { |
| 62 | + <?php foreach ($units[$defaultCategory] as $toUnitCode => $toUnitName) { |
62 | 63 | echo "<option value=\"{$toUnitCode}\" " . ((isset($_POST['toUnit']) && $_POST['toUnit'] === $toUnitCode) ? 'selected' : '') . ">{$toUnitName}</option>", PHP_EOL; |
63 | 64 | } ?> |
64 | 65 | </select> |
|
73 | 74 |
|
74 | 75 | <?php |
75 | 76 | /** If the user has submitted the form, then we need to calculate the value and display the result */ |
76 | | -if (isset($_POST['submit'])) { |
| 77 | +if (isset($_POST['quantity'], $_POST['fromUnit'], $_POST['toUnit'])) { |
77 | 78 | $quantity = $_POST['quantity']; |
78 | 79 | $fromUnit = $_POST['fromUnit']; |
79 | 80 | $toUnit = $_POST['toUnit']; |
80 | | - $result = ConvertUOM::CONVERT($quantity, $fromUnit, $toUnit); |
| 81 | + if (!is_numeric($quantity)) { |
| 82 | + $helper->log('Quantity is not numeric'); |
| 83 | + } elseif (isset($units[$_POST['category']][$fromUnit], $units[$_POST['category']][$toUnit])) { |
| 84 | + /** @var float|string */ |
| 85 | + $result = ConvertUOM::CONVERT($quantity, $fromUnit, $toUnit); |
81 | 86 |
|
82 | | - echo "{$quantity} {$units[$_POST['category']][$fromUnit]} is {$result} {$units[$_POST['category']][$toUnit]}", PHP_EOL; |
| 87 | + $helper->log("{$quantity} {$units[$_POST['category']][$fromUnit]} is {$result} {$units[$_POST['category']][$toUnit]}"); |
| 88 | + } else { |
| 89 | + $helper->log('Please enter quantity and select From Unit and To Unit'); |
| 90 | + } |
| 91 | +} else { |
| 92 | + $helper->log('Please enter quantity and select From Unit and To Unit'); |
83 | 93 | } |
0 commit comments