我想創建一個接受兩個數字的表單,然后將這兩個數字作為引數傳遞給 php 計算器類,然后根據請求對它們進行加減乘除(單擊它們各自的按鈕)。我已經成功地創建了計算器類和表單,但我不知道單獨呼叫帶有數字作為引數的函式。這是我不完整的代碼
<?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新手
uj5u.com熱心網友回復:
按鈕表單欄位并沒有多大作用——例如,當將資料發布到服務器時,它們實際上并沒有起作用。它們主要是為了與 JavaScript 一起使用而存在的。您要使用的是[單選按鈕][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
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/452252.html
上一篇:為鍵的每個值添加元素-JSON
