<?php

/* ======================================================
   PHP Calculator example using "sticky" form (Version 1)
   ======================================================

   Author : P Chatterjee (adopted from an original example written by C J Wallace)
   Extended by

   Purpose : To multiply 2 numbers passed from a HTML form and display the result.

   input:
      x, y : numbers
      calc : Calculate button pressed


   Date: 30 Sep 2016

*/

// grab the form values from $_HTTP_POST_VARS hash
extract($_GET);

// first compute the output, but only if data has been input
   if(isset($calc) && $operation == "multiply") { // $calc exists as a variable
      $prod = $x * $y;
      $result = "$x x $y = $prod";
   } elseif(isset($calc) && $operation == "add") {
     $prod = $x + $y;
     $result = "$x + $y = $prod";
   } elseif(isset($calc) && $operation == "subtract") {
     $prod = $x - $y;
     $result = "$x - $y = $prod";
   } elseif(isset($calc) && $operation == "divide") {
     if ($y == 0) {
       $result = "$x &divide; $y = &infin;";
     } else {
       $prod = $x / $y;
       $result = "$x &divide; $y = $prod";
     }
   } else { // set defaults
      $x=0;
      $y=0;
      $prod=0;
   }
?>

<html>
   <head>
      <title>PHP Calculator Example</title>
      <link href="https://fonts.googleapis.com/css?family=Karla|Roboto+Mono" rel="stylesheet">
      <link rel="stylesheet" href="style.css" media="screen" title="no title" charset="utf-8">
   </head>

   <body>
      <div class="container">


      <h3>PHP Calculator (Version 4)</h3>
      <p>This program can perform four basic arithmetic functions.</p>
      <p>It can add, subtract, divide and multiply values.</p>

      <form method="get" action="<?php print $_SERVER['PHP_SELF']; ?>">

         x = <input type="text" name="x" size="5" value="<?php   print $x; ?>"/>
         <select class="" name="operation">
           <option value="add" <?php if(isset($operation) && $operation == "add"){ echo 'selected="selected"';} ?>>+</option>
           <option value="subtract" <?php if(isset($operation) && $operation == "subtract"){ echo 'selected = "selected"';} ?>>-</option>
           <option value="multiply" <?php if(isset($operation) && $operation == "multiply"){ echo 'selected = "selected"';} ?>>x</option>
           <option value="divide" <?php if(isset($operation) && $operation == "divide"){ echo 'selected = "selected"';} ?>>&divide;</option>
         </select>
         y =  <input type="text" name="y" size="5" value="<?php  print $y; ?>"/>

         <input class="button" type="submit" name="calc" value="Calculate"/>
         <input class="button" type="submit" name="clear" value="Clear"/>
      </form>

      <!-- print the result -->
      <p class="result"><?php
      if(isset($calc)) {

         print $result;

      }  else {
        echo "Result will show here.";
      }
      ?></p>

      <pre>
        <?php
          if (isset($_GET['operation'])) {
            echo "The operation value exists.";
          }
        ?>
      </pre>
      </div>
   </body>
</html>
