Advertisement
michaelyuen

Untitled

Aug 26th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.21 KB | None | 0 0
  1. <?php
  2.  
  3. ini_set('display_errors', 1);
  4.  
  5. $result = '';
  6.  
  7. if(isset($_POST['calculate'])) {
  8.  
  9.     if(empty($_POST['firstValue']) && empty($_POST['lastValue'])) { // here $_POST
  10.              echo  "<script>alert('Please enter values')</script>";
  11.         }
  12.  
  13.         elseif (empty($_POST['firstValue'])) {// here $_POST  
  14.              echo  "<script>alert('Please enter first value')</script>";
  15.         }
  16.         elseif (empty($_POST['lastValue'])) {// here $_POST
  17.              echo  "<script>alert('Please enter second value')</script>";
  18.         }  
  19.         elseif (filter_var($_POST['firstValue'], FILTER_VALIDATE_INT) === false) {// here $_POST
  20.              echo  "<script>alert('First value must be numeric')</script>";
  21.         }
  22.         elseif (filter_var($_POST['lastValue'], FILTER_VALIDATE_INT) === false) {// here $_POST
  23.              echo  "<script>alert('Last value must be numeric')</script>";
  24.         }
  25.                
  26.         else {
  27.                    
  28.                     $operator = $_POST['operator'];
  29.  
  30.                     switch ($operator) {
  31.                             case '+':
  32.                                             $add1 = $_POST['firstValue'];
  33.                                             $add2 = $_POST['lastValue'];
  34.                                             $result = $add1 + $add2;
  35.                                             break; // here
  36.              
  37.                             case '-':
  38.                                             $add1 = $_POST['firstValue'];
  39.                                             $add2 = $_POST['lastValue'];
  40.                                             $result = $add1 - $add2;
  41.                                             break; // here
  42.                             case '*':
  43.                                             $add1 = $_POST['firstValue'];
  44.                                             $add2 = $_POST['lastValue'];
  45.                                             $result = $add1 * $add2;
  46.                                             break; // here
  47.                             case '/':
  48.                                             $add1 = $_POST['firstValue'];
  49.                                             $add2 = $_POST['lastValue'];
  50.                                             $result = $add1 / $add2;
  51.                                             break;
  52.              
  53.                             default:
  54.                                             $result = '';
  55.                                             echo "not accepted";
  56.                                             break;
  57.                     }
  58.                    
  59.         }
  60.  
  61. }
  62.  
  63.  
  64. ?>
  65. <form action="" method="POST"> <!-- here method="POST" -->
  66.     <table style="border: groove #00FF99">
  67.         <tr>
  68.  
  69.         <td style="background-color: aqua; color: red; font-family: Arial;">Enter first number</td>
  70.         <td colspan="1"><input type="text" name="firstValue" style="color: red;"></td>
  71.  
  72.         <tr>
  73.             <td style="color:burlywood; font-family: Arial;">Select Operator</td>
  74.             <td>
  75.                 <select name="operator" style="width: 63px;">
  76.                     <option>+</option>
  77.                     <option>-</option>
  78.                     <option>*</option>
  79.                     <option>/</option>
  80.                 </select>
  81.             </td>
  82.         </tr>
  83.  
  84.         <tr>
  85.  
  86.         <td style="background-color: aqua; color: red; font-family: Arial;">Enter second number</td>
  87.         <td class="auto-style5"><input type="text" name="lastValue" style="color: red;"></td>
  88.  
  89.         </tr>
  90.  
  91.                 <tr>
  92.                     <td></td>
  93.                     <td><input type="submit" name="calculate" value="Calculate" style="color: wheat; background-color: rosybrown;">         </td>
  94.                 </tr>
  95.  
  96.                 <tr>
  97.                     <td style="background-color: aqua; color: red;">Output =</td>
  98.                     <td style="color: darkblue;"><?php echo $result; ?></td>
  99.                 </tr>
  100.  
  101.                 </tr>
  102.             </table>
  103.         </form>
  104.  
  105.    
  106.  
  107.  
  108.  
  109.  
  110. </body>
  111. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement