Advertisement
Guest User

Untitled

a guest
Mar 24th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.79 KB | None | 0 0
  1. <?php
  2.   $title = "Lab 6 - Self-referring Forms w/ Data Validation";
  3.   $file = "lab6.php";
  4.   $description = "This lab page will have an HTML form that refers back to itself, and will use PHP to handle data validation.";
  5.   $date = "Date: March 23, 2018";
  6.   $banner = "Lab 6: Self-referring Forms w/ Data Validation";
  7.   include 'header.php'; //moved this to here
  8.   define('MAX_ITERATIONS','100');
  9.  
  10.   $start = "";
  11.   $stop = "";
  12.   $incr = "";
  13.   $error = "";
  14.   $output = ""; //changed this from $result
  15.  
  16.   if ($_SERVER["REQUEST_METHOD"] == "post")
  17.   {
  18.     $start = trim($_POST["startTemp"]);
  19.     $stop = trim($_POST["stopTemp"]);
  20.     $incr = trim($_POST["incrRate"]);
  21.  
  22.     if(!isset($start) || $start == "")
  23.     {
  24.       $error .= "You need to enter a starting value.";
  25.     }
  26.     else if(!is_numeric($start))
  27.     {
  28.       $error .= "You need to enter a number in the initial value box.";
  29.       $start = "";
  30.     }
  31.  
  32.     if(!isset($stop) || $stop == "")
  33.     {
  34.       $error .= "You need to enter an ending value.";
  35.     }
  36.     else if(!is_numeric($stop))
  37.     {
  38.       $error .= "You need to enter a number in the final value box.";
  39.       $stop = "";
  40.     }
  41.  
  42.     if(!isset($incr) || $incr == "")
  43.     {
  44.       $error .= "You need to enter an increment rate.";
  45.     }
  46.     else if(!is_numeric($incr))
  47.     {
  48.       $error .= "You need to enter a number in the increment rate box.";
  49.       $incr = "";
  50.     }
  51.  
  52.     if((($stop - $start)/$incr) > MAX_ITERATIONS)
  53.     {
  54.       $error .= "The maximum number of iterations has been exceded. Please enter a lower increment rate";
  55.     }
  56.     else
  57.     {
  58.       if($error == "" && $_SERVER["REQUEST_METHOD"] == "post")
  59.       {
  60.         $output = "<table border='2px'>";
  61.         $output .= "<tr>";
  62.         $output .= "<th>Celcius</th>";
  63.         $output .= "<th>Fahrenheit</tr>";
  64.         $output .= "</tr>";
  65.         for($celc = $start; $celc <= $stop; $celc += $incr)
  66.         {
  67.           $fahr = (9.0 / 5.0) * $celc + 32;
  68.           $output .= "<tr><td align='center'>$celc&deg;</td><td align='center'>$fahr&deg;</td></tr>";
  69.         }
  70.         $output .= "</table>"; //missed ;
  71.       }
  72.       else
  73.       {
  74.         $error .= "<br/>Please try again.";
  75.       }
  76.     }
  77.   }
  78. ?>
  79.  
  80. <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
  81.   Enter your initial value: <input type="text" name="startTemp" value="<?php echo $start; ?>" size = "5" /><br/>
  82.   Enter your final value: <input type = "text" name = "stopTemp" value = "<?php echo $stop; ?>" size = "5" /><br/>
  83.   Enter your initial value: <input type = "text" name = "incrRate" value = "<?php echo $incr; ?>" size = "5" /><br/>
  84.   <input type = "submit" value = "Create Temperature Conversion Table" />
  85. </form>
  86.  
  87. <h2><?php echo $output; ?></h2>
  88. <h3><?php echo $error; ?></h3>
  89. <?php include 'footer.php'; ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement