Home > Enterprise >  How to create a form that accepts two numbers and then pass them as arguments to a php calculator cl
How to create a form that accepts two numbers and then pass them as arguments to a php calculator cl

Time:03-29

I want to create a form that accepts two numbers, then pass the two numbers as arguments to php calculator class and then add, subtract, multiply and divide them on request(on clicking their respective buttons). I have successfully created the calculator class and the form but I don't know hot to individually call the functions with the numbers as arguments. Here is my incomplete code

<?php
class MyCalculator {
public $_fval, $_sval;
public function __construct( $fval, $sval ) {
$this->_fval = $fval;
$this->_sval = $sval;
}
public function add() {
return $this->_fval   $this->_sval;
}
public function subtract() {
return $this->_fval - $this->_sval;
}
public function multiply() {
return $this->_fval * $this->_sval;
}
public function divide() {
return $this->_fval / $this->_sval;
}
}
?>

<html>
<body>

<form action=<?php echo $_SERVER['PHP_SELF'] ?> method='post'>
a: <input type="number" name="num-a"><br><br>

b: <input type="number" name="num-b"><br><br>

   <input type='button' value='add'>
   <input type='button' value='multiply'>
   <input type='button' value='subtract'>
   <input type='button' value='divide'>
</form>

</body>
</html>

<?php
 $x = $_POST['num-a']; 
 $y = $_POST['num-b']; 
?>

Please help, I am new to php

CodePudding user response:

Button form fields don't do much - they don't really work when posting data to a server, for example. They mostly exist to be used with JavaScript. What you want to use is a [radio button][1].

Then you should be able to print out your results for the end user. Below is an example of how you might be able to do this.

<?php
class MyCalculator {
    public $_fval, $_sval;
    public function __construct( $fval, $sval ) {
        $this->_fval = $fval;
        $this->_sval = $sval;
    }
    public function add() {
        return $this->_fval   $this->_sval;
    }
    public function subtract() {
        return $this->_fval - $this->_sval;
    }
    public function multiply() {
        return $this->_fval * $this->_sval;
    }
    public function divide() {
        return $this->_fval / $this->_sval;
    }
}
?>

<html>
<body>

<form action=<?php echo $_SERVER['PHP_SELF'] ?> method='post'>
<p>
    <label for="num-a">a</label>: <input type="number" id="num-a" name="num-a">
</p>
<p>
    <label for="num-b">b</label>: <input type="number" id="num-b" name="num-b">
</p>

   <label for="operation-add">Add</label>
   <input type='radio' name="operation" value='add' id="operation-add">
   
   <label for='operation-multiply'>Multiply</label>
   <input type='radio' name="operation" value='multiply' id='operation-multiply'>
   
   <label for='operation-subtract'>Subtract</label>
   <input type='radio' name="operation" value='subtract' id='operation-subtract'>

   <label for='operation-divide'>Divide</label>
   <input type='radio' name="operation" value='divide' id='operation-divide'>
</form>

</body>
</html>

<?php
 // You need to verify the values have been submitted before using them
if ( isset($_POST['num-a']) && isset($_POST['num-b']) &&  isset($_POST['operation']) ):
 $x = $_POST['num-a']; 
 $y = $_POST['num-b']; 
 $operation = $_POST['operation'];

//instantiate your calculator
 $calc = new MyCalculator($x, $y);

// then print out the results of the calculation
 echo "<p>Your result is: <strong>";
 switch ($operation) {
   case "add":
     echo $calc->add();
     break;
   case "subtract":
     echo $calc->subtract();
     break;
   case "divide":
     echo $calc->divide();
     break;
   case "add":
     echo $calc->multiply();
     break;
   default:
     echo "Unsupported operation used";
     break;
 }
 echo "</strong></p>";
}
?>


  [1]: https://www.w3schools.com/tags/att_input_type_radio.asp
  • Related