Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.13 KB | None | 0 0
  1. <html>
  2.  
  3. <body>
  4. <form action="#" method="post">
  5. Please enter distance: <input type="text" name="distance" /><br/>
  6. Kms:<input type="radio" name="units" value="kms" /><br/>
  7. Miles:<input type="radio" name="units" value="miles" /> <br/>
  8. <input type="submit" value="submit">
  9. </form>
  10. </body>
  11.  
  12. </html>
  13. <html>
  14. <body>
  15. <?php
  16. // Check to see if the request method from the form is post
  17. if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  18.  
  19. //declare the POST variables
  20. $distance = $_POST['distance'];
  21. $units = $_POST['units'];
  22.  
  23. // check if units are kms or KMS (mostly just for error handling)
  24. if ($units == 'kms' || $units == 'KMS') {
  25. $miles = $distance /1.60934;
  26. echo 'The distance you have entered is equivalent to ' .round($miles).'
  27. miles.';
  28. } else if ($units == 'miles' || $units == 'mps') { // check to see if units are miles or mps (error handling)
  29. $kms = $distance * 1.60934;
  30. echo 'The distance you have entered is equivalent to ' .round($kms).'
  31. kilometers.';
  32. } else { // if an invalid unit has been passed through then throw this error.
  33. echo 'Received invalid unit. Please use kms, KMS, miles or mps. You used: '. $units;
  34. }
  35. }
  36.  
  37. /*
  38. Why if/else if/else?
  39. because we have 2 determined inputs and there is an edge case for something to go wrong so we use the last else
  40. as an error handler. Typically we would throw an exception but theres no need for that here.
  41.  
  42. What is $_SERVER[] ?
  43. $_SERVER[] is the magic php server object to check if there are get/post/put/patch requests to the server to do something with
  44. data. Typically we would also use an isset() method to checkl to see if the post is set but no need for that here.
  45. */
  46. ?>
  47.  
  48. </body>
  49.  
  50. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement