<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="Generator" content="EditPlus®">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<title>计算器</title>
</head>
<body>
<form action="test078Result.php" method="post">
<table width="300px" border="0">
<td>第一个数</td> <td><input type="text" name="num1"/></td>
<tr/>
<td>第二个数</td> <td><input type="text" name="num2"/></td>
<tr/>
<td>运算符</td>
<td>
<select name="oper"/>
<option value="+">+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
</select>
</td>
<tr>
<td colspan="2"><input type="submit" value="计算结果"/></td>
</tr>
</table>
</form>
</body>
</html>
<?php
$num1=$_REQUEST['num1'];
//2、接收num2
$num2=$_REQUEST['num2'];
//3、接收运算符
$oper=$_REQUEST['oper'];
class OperService{
public $numOne;
public $numTwo;
public $operShow;
public function setData($num1,$num2,$oper){
$this->numOne = $num1;
$this->numTwo = $num2;
$this->operShow = $oper;
}
public function getLastShow(){
$lastShow = 0;
switch($this->operShow){
case "+":
$lastShow = $this->numOne + $this->numTwo;
break;
case "-":
$lastShow = $this->numOne - $this->numTwo;
break;
case "*":
$lastShow = $this->numOne * $this->numTwo;
break;
case "/":
$lastShow = $this->numOne / $this->numTwo;
break;
}
return $lastShow;
}
}
$operService = new OperService();
$operService->setData($num1,$num2,$oper);
print $operService->getLastShow();
?>
结果:按要求输出。。。。
新版:
在html页面增加数据校验:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="Generator" content="EditPlus®">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<title>计算器</title>
//增加js数据检验--有这个值不对的话浏览器就有弹窗了
<script type="text/javascript">
function check(){
var num1 = document.getElementById("num1").value;
var num2 = document.getElementById("num2").value;
//window.alert(num1+" "+num2);
if(isNaN(num1)||isNaN(num1)){
window.alert("请确认输入的值是否整数");
return false;
}
}
</script>
</head>
<body>
<form action="test078Result.php" method="post" onsubmit="return check()">
<table width="300px" border="0">
<td>第一个数</td> <td><input type="text" id="num1" name="num1"/></td>
<tr/>
<td>第二个数</td> <td><input type="text" id="num2" name="num2"/></td>
<tr/>
<td>运算符</td>
<td>
<select name="oper"/>
<option value="+">+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
</select>
</td>
<tr>
<td colspan="2"><input type="submit" value="计算结果"/></td>
</tr>
</table>
</form>
</body>
</html>
结果处理界面增加数据接收判断:
<?php
if(isset($_REQUEST['num1'])){
$num1=$_REQUEST['num1'];
}
if(isset($_REQUEST['num2'])){
//2、接收num2
$num2=$_REQUEST['num2'];
}
if(isset($_REQUEST['oper'])){
//3、接收运算符
$oper=$_REQUEST['oper'];
}
class OperService{
public $numOne;
public $numTwo;
public $operShow;
public function setData($num1,$num2,$oper){
$this->numOne = $num1;
$this->numTwo = $num2;
$this->operShow = $oper;
}
public function getLastShow(){
$lastShow = 0;
switch($this->operShow){
case "+":
$lastShow = $this->numOne + $this->numTwo;
break;
case "-":
$lastShow = $this->numOne - $this->numTwo;
break;
case "*":
$lastShow = $this->numOne * $this->numTwo;
break;
case "/":
$lastShow = $this->numOne / $this->numTwo;
break;
}
return $lastShow;
}
}
$operService = new OperService();
$operService->setData($num1,$num2,$oper);
print $operService->getLastShow();
?>