php一个表单里多个按钮,怎么知道是哪个对应的(简单的四则运算)
刚学习php,怎么分开进行四则运算,不受影响,还需要哪个地方进行添加,感谢~!<?phpfunctionmake_safe($variable){return$varia...
刚学习php,怎么分开进行四则运算,不受影响,还需要哪个地方进行添加,感谢~!
<?php
function make_safe($variable) {
return $variable + 0;}
$numa=make_safe($_POST["numa"]);
$numb=make_safe($_POST["numb"]);
$numc=$numa+$numb;
$numa1=make_safe($_POST["numa1"]);
$numb1=make_safe($_POST["numb1"]);
$numc1=$numa1-$numb1;
$numa2 = make_safe($_POST["numa2"]);
$numb2 = make_safe($_POST["numb2"]);
$numc2 = $numa2 * $numb2;
$numa3=make_safe($_POST["numa3"]);
$numb3=make_safe($_POST["numb3"]);
if ($numb3!=0) {
$numc3=$numa3/$numb3;
}
elseif ($numb3==0) {
}
?>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>四则运算</title>
</head>
<body style=" text-align:center " >
<form id="form" method="post" style="margin-top: 100px," >;
<h2>简单计算器 </h2>
<input type="text" name="numa" value="<?php echo (isset($numa)) ? $numa : '';?>"size="10" />
+
<input type="text" name="numb" value="<?php echo (isset($numb))? $numb :'';?>" size="10" />
=
<input type="text"name="numc" value="<?php echo (isset($numc)) ? $numc : '';?>" size="10" />
<input type="submit" name="sub" value="计算" ' />
<br/>
</form>
<br/>
<form method="post" style="margin-top: 100px," >
<input type="text" name="numa1" value="<?php echo (isset($numa1)) ? $numa1: '';?>"size="10" />
-
<input type="text" name="numb1" value="<?php echo (isset($numb1))? $numb1:'';?>" size="10" />
=
<input type="text" name="numc1" value="<?php echo (isset($numc1)) ? $numc1:'';?>" size="10" />
<input type="submit" name="sub1" value="计算" /><br/>
</form>
<br/>
<form method="post" style="margin-top: 100px," >
<input type="text" name="numa2" value="<?php echo (isset($numa2)) ? $numa2: '';?>"size="10" />
*
<input type="text" name="numb2" value="<?php echo (isset($numb2))? $numb2:'';?>" size="10" />
=
<input type="text"name="numc2" value="<?php echo (isset($numc2)) ? $numc2:'';?>" size="10" />
<input type="submit" name="sub2" value="计算" /><br/>
</form>
<br/>
<form method="post" style="margin-top: 100px," >
<input type="text" name="numa3" value="<?php echo (isset($numa3)) ? $numa3: '';?>"size="10" />
/
<input type="text" name="numb3" value="<?php echo (!empty($numb3))? $numb3:'';?>" size="10" />
=
<input type="text" name="numc3" value="<?php echo (isset($numc3)) ? $numc3: '';?>" size="10" />
<input type="submit" name="sub3" value="计算" /><br/>
</form>
</body>
</html> 展开
<?php
function make_safe($variable) {
return $variable + 0;}
$numa=make_safe($_POST["numa"]);
$numb=make_safe($_POST["numb"]);
$numc=$numa+$numb;
$numa1=make_safe($_POST["numa1"]);
$numb1=make_safe($_POST["numb1"]);
$numc1=$numa1-$numb1;
$numa2 = make_safe($_POST["numa2"]);
$numb2 = make_safe($_POST["numb2"]);
$numc2 = $numa2 * $numb2;
$numa3=make_safe($_POST["numa3"]);
$numb3=make_safe($_POST["numb3"]);
if ($numb3!=0) {
$numc3=$numa3/$numb3;
}
elseif ($numb3==0) {
}
?>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>四则运算</title>
</head>
<body style=" text-align:center " >
<form id="form" method="post" style="margin-top: 100px," >;
<h2>简单计算器 </h2>
<input type="text" name="numa" value="<?php echo (isset($numa)) ? $numa : '';?>"size="10" />
+
<input type="text" name="numb" value="<?php echo (isset($numb))? $numb :'';?>" size="10" />
=
<input type="text"name="numc" value="<?php echo (isset($numc)) ? $numc : '';?>" size="10" />
<input type="submit" name="sub" value="计算" ' />
<br/>
</form>
<br/>
<form method="post" style="margin-top: 100px," >
<input type="text" name="numa1" value="<?php echo (isset($numa1)) ? $numa1: '';?>"size="10" />
-
<input type="text" name="numb1" value="<?php echo (isset($numb1))? $numb1:'';?>" size="10" />
=
<input type="text" name="numc1" value="<?php echo (isset($numc1)) ? $numc1:'';?>" size="10" />
<input type="submit" name="sub1" value="计算" /><br/>
</form>
<br/>
<form method="post" style="margin-top: 100px," >
<input type="text" name="numa2" value="<?php echo (isset($numa2)) ? $numa2: '';?>"size="10" />
*
<input type="text" name="numb2" value="<?php echo (isset($numb2))? $numb2:'';?>" size="10" />
=
<input type="text"name="numc2" value="<?php echo (isset($numc2)) ? $numc2:'';?>" size="10" />
<input type="submit" name="sub2" value="计算" /><br/>
</form>
<br/>
<form method="post" style="margin-top: 100px," >
<input type="text" name="numa3" value="<?php echo (isset($numa3)) ? $numa3: '';?>"size="10" />
/
<input type="text" name="numb3" value="<?php echo (!empty($numb3))? $numb3:'';?>" size="10" />
=
<input type="text" name="numc3" value="<?php echo (isset($numc3)) ? $numc3: '';?>" size="10" />
<input type="submit" name="sub3" value="计算" /><br/>
</form>
</body>
</html> 展开
2014-08-14
展开全部
你这种设计相当不合理!
再说,单纯为了计算数, 何必php?
js不是更简单方便?
<?php
error_reporting(E_ALL ^ E_NOTICE); //忽略错误
function Select( $Num = 1 ) {
if ( $Num == 1 && empty( p('fu') ) ) {
return ' selected';
}else{
return p('fu') == $Num ? ' selected' : '';
}
}
function p( $key ) {
return $_POST[ $key ];
}
if ( p('mm') == 1 ) {
$numa = p('numa');
$numb = p('numb');
$numc = $numa;
$fu = p('fu');
switch( $fu ) {
case 1:
$numc += $numb;
break;
case 2:
$numc -= $numb;
break;
case 3:
$numb > 0 ? $numc *= $numb : $numc = 0;
break;
default:
$numb > 0 ? $numc /= $numb : $numc = 0;
}
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>简易计算器</title>
</head>
<body style="margin:100px auto;width:800px">
<h2>简单计算器</h2>
<form method="post">
<input type="number" name="numa" value="<?php echo $numa ?>">
<select name="fu">
<option value="1"<?php echo Select(1) ?>>+</option>
<option value="2"<?php echo Select(2) ?>>-</option>
<option value="3"<?php echo Select(3) ?>>*</option>
<option value="4"<?php echo Select(4) ?>>/</option>
</select>
<input type="number" name="numb" value="<?php echo $numb ?>">
=
<input type="text" name="numc" value="<?php echo $numc ?>">
<button type="submit" name="mm" value="1">计算</button>
</form>
</body>
</html>
这样是不是更恰当啊?
追问
谢谢你,我是PHP初学者,就是练习不用下拉框来处理的,上面是导师故意安排练习的,谢谢你,已经处理OK了
追答
怎么导师嘛!
开发程序,在不影响程序运行的情况下, 代码是越清晰、越简洁越好!
那能像这样弄呢!
这也叫导师。。。。
况且,下拉框只是一个方式而已, 要用其他的方式,方法有的是, 就看自己各种知识掌握得如何
!
就如楼上哪位朋友回答的, 你年html最基本的东西都没弄顺畅, 还php?
就算是学习php, 对html不要求精通, 但至少有有最最基本的了解和运用啊?
否则, php学了也是白学!
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询