如何创建一个接受两个数字的表单,然后将它们作为参数传递给 php 计算器 class?

How to create a form that accepts two numbers and then pass them as arguments to a php calculator class?

我想创建一个接受两个数字的表单,然后将这两个数字作为参数传递给 php 计算器 class 然后根据要求(点击他们各自的按钮)。我已经成功地创建了计算器 class 和表格,但我不知道以数字作为参数单独调用函数的热度。这是我不完整的代码

<?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']; 
?>

请帮忙,我是新手php

按钮表单字段作用不大 - 例如,它们在将数据发布到服务器时实际上不起作用。它们的存在主要是为了与 JavaScript 一起使用。您要使用的是 [radio button][1].

然后您应该能够为最终用户打印出您的结果。下面是您可以如何执行此操作的示例。

<?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