如何從表單中獲取值$ val1,$ val2?從表單中獲取值(OOP php)
可能需要使用類似這樣的設計:
$obj->Calculator($_POST['val1'], $_POST['val1']);
但..哪裏粘貼?
<?php
class Calculator {
private $_val1 , $_val2;
public function __construct($val1, $val2){
$this->_val1 = $val1;
$this->_val2 = $val2;
}
public function add(){
return $this->_val1 + $this->_val2;
}
public function subtract(){
return $this->_val1 - $this->_val2;
}
public function multiply(){
return $this->_val1 * $this->_val2;
}
public function divide() {
return $this->_val1/$this->_val2;
}
}
$calc = new Calculator(3,4);
echo "<p>3 + 4 = ".$calc->add(). "</p>";
?>
<html>
<head>
<title>Calculator</title>
</head>
<body>
<form action='' method='POST'>
Enter Number:
<input type='text' name='val1' value="<?=$val1;?>">
<input type='text' name='val2' value="<?=$val2;?>">
<input type='submit' name='submit' value='Calculate'>
</form>
</body>
</html>
'$計算值=新計算器($ _ POST [ 'VAL1'],$ _POST [ 'VAL1']);'? –
'new Calculator($ _ POST ['val1'],$ _POST ['val1']);' –