Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <html>
- <body>
- <form action="#" method="post">
- Please enter distance: <input type="text" name="distance" /><br/>
- Kms:<input type="radio" name="units" value="kms" /><br/>
- Miles:<input type="radio" name="units" value="miles" /> <br/>
- <input type="submit" value="submit">
- </form>
- </body>
- </html>
- <html>
- <body>
- <?php
- // Check to see if the request method from the form is post
- if ($_SERVER['REQUEST_METHOD'] == 'POST') {
- //declare the POST variables
- $distance = $_POST['distance'];
- $units = $_POST['units'];
- // check if units are kms or KMS (mostly just for error handling)
- if ($units == 'kms' || $units == 'KMS') {
- $miles = $distance /1.60934;
- echo 'The distance you have entered is equivalent to ' .round($miles).'
- miles.';
- } else if ($units == 'miles' || $units == 'mps') { // check to see if units are miles or mps (error handling)
- $kms = $distance * 1.60934;
- echo 'The distance you have entered is equivalent to ' .round($kms).'
- kilometers.';
- } else { // if an invalid unit has been passed through then throw this error.
- echo 'Received invalid unit. Please use kms, KMS, miles or mps. You used: '. $units;
- }
- }
- /*
- Why if/else if/else?
- because we have 2 determined inputs and there is an edge case for something to go wrong so we use the last else
- as an error handler. Typically we would throw an exception but theres no need for that here.
- What is $_SERVER[] ?
- $_SERVER[] is the magic php server object to check if there are get/post/put/patch requests to the server to do something with
- data. Typically we would also use an isset() method to checkl to see if the post is set but no need for that here.
- */
- ?>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement