[PHP] 纯文本查看 复制代码 <?php
// 是否提交动作
if (isset($_POST['submit'])){
$num1 = $_POST['num1'];
$num2 = $_POST['num2'];
$bz = true;
$errorMsg = '存在以上问题:<br/>';
if ($num1 == ""){
$bz = false;
$errorMsg = $errorMsg."第一个数不能为空<br/>";
}else if (!is_numeric($num1)){
$errorMsg = $errorMsg."第一个数不是数字<br/>";
$bz = false;
}
if ($num2 == ""){
$errorMsg = $errorMsg."第二个数不能为空<br/>";
$bz = false;
}else if (!is_numeric($num2)){
$errorMsg = $errorMsg."第二个数不是数字<br/>";
$bz = false;
}
if ($bz){
// 标记为真则执行
$result = '';
$ysf = $_POST['ysf'];
switch($ysf){
case '+':{
$result = $num1 + $num2;
}break;
case '-':{
$result = $num1 - $num2;
}break;
case 'x':{
$result = $num1 * $num2;
}break;
case '/':{
$result = $num1 / $num2;
}break;
case '%':{
$result = $num1 % $num2;
}break;
default:break;
}
}else{
// 输出问题所在
echo $errorMsg;
}
}else{
echo '刷新';
}
?>
<html>
<head>
<title>简单计算器</title>
</head>
<body>
<table border="0" width="400" align="center">
<form action="index.php" method="post">
<caption>简单计算器</caption>
<tr>
<td>
<input type="text" size="4" name="num1" align="center" value = "<?php echo $num1?>"/>
</td>
<td>
<select name="ysf" value=<?php echo $ysf ?>>
<option <?php if ($ysf == '+') echo 'selected'?> value="+">+</option>
<option <?php if ($ysf == '-') echo 'selected'?> value="-">-</option>
<option <?php if ($ysf == 'x') echo 'selected'?> value="x">x</option>
<option <?php if ($ysf == '/') echo 'selected'?> value="/">/</option>
<option <?php if ($ysf == '%') echo 'selected'?> value="%">%</option>
</select>
</td>
<td>
<input type="text" size="5" name="num2" value = "<?php echo $num2?>"/>
</td>
<td>
<input type="submit" name="submit" value="="/>
</td>
<td>
<?php echo $result?>
</td>
</tr>
</form>
</table>
</body>
|