新手问题,用php 写简单计算器,功能实现不了,是代码错误还是缺少什么东西?代码如下,用notepad+++写的
<?phpif(isset($_POST['sub'])){$a=0;if($_POST['sel']=='+'){$a=$_POST['num1']+$_POST['n...
<?php if(isset($_POST['sub'])){ $a=0; if($_POST['sel']=='+'){ $a=$_POST['num1']+$_POST['num2']; }elseif($_POST['sel']=='-'){ $a=$_POST['num1']-$_POST['num2']; }elseif($_POST['sel']=='*'){ $a=$_POST['num1']*$_POST['num2']; }elseif($_POST['sel']=='/'){ $a=$_POST['num1']/$_POST['num2']; } }?><!DOCTYPE html><html> <head> <meta charset="utf-8" /> <style type="text/css"> table{ width:400px; height:200px; margin-left:350px; margin-top:100px; background-color:#999999; text-align:center; } </style> </head> <body> <table> <form nethod="post" action=""> <tr> <td> <input type="number" name="num1" value="<?php echo $_POST['num1']?>"/> </td> <td> <select name="sel"> <option value="+" <?php echo $_POST['sel']="+"?"selected":""?> +</option> <option value="-" <?php echo $_POST['sel']=="-"?"selected":""?> -</option> <option value="*" <?php echo $_POST['sel']=="*"?"selected":""?> *</option> <option value="/" <?php echo $_POST['sel']=="/"?"selected":""?> /</option> </select> </td> <td> <input type="number" name="num2" value="<?php echo $_POST['num2']?>" /> </td> <td> <input type="submit" name="sub" value="="/> </td> <td> <input type="text" name="result" value="<?php echo $a?>"/> </td> </tr> </form> </table> </body></html>
展开
2个回答
展开全部
<?php
if(isset($_POST['sub'])){
// $a=0;
if($_POST['sel']=='+'){
$a=$_POST['num1']+$_POST['num2'];
}elseif($_POST['sel']=='-'){
$a=$_POST['num1']-$_POST['num2'];
}elseif($_POST['sel']=='*'){
$a=$_POST['num1']*$_POST['num2'];
}elseif($_POST['sel']=='/'){
$a=$_POST['num1']/$_POST['num2'];
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<style type="text/css">
table{
width:400px;
height:200px;
margin-left:350px;
margin-top:100px;
background-color:#999999;
text-align:center;
}
</style>
</head>
<body>
<table>
<form method="post" action="">
<tr>
<td>
<input type="number" name="num1" value="<?php if(isset($_POST['num1'])){ echo $_POST['num1']; } ?>"/>
</td>
<td>
<select name="sel">
<option value="+" <?php if(isset($_POST['sel'])){ echo $_POST['sel']="+"?"selected":""; } ?> > +</option>
<option value="-" <?php if(isset($_POST['sel'])) echo $_POST['sel']=="-"?"selected":"";?> >-</option>
<option value="*" <?php if(isset($_POST['sel'])) echo $_POST['sel']=="*"?"selected":"";?> >*</option>
<option value="/" <?php if(isset($_POST['sel'])) echo $_POST['sel']=="/"?"selected":"";?> >/</option>
</select>
</td>
<td>
<input type="number" name="num2" value="<?php if(isset($_POST['num2'])){echo $_POST['num2'];} ?>" />
</td>
<td>
<input type="submit" name="sub" value="="/>
</td>
<td>
<input type="text" name="result" value="<?php if(isset($a)){echo $a;} ?>"/>
</td>
</tr>
</form>
</table>
</body>
</html>
if(isset($_POST['sub'])){
// $a=0;
if($_POST['sel']=='+'){
$a=$_POST['num1']+$_POST['num2'];
}elseif($_POST['sel']=='-'){
$a=$_POST['num1']-$_POST['num2'];
}elseif($_POST['sel']=='*'){
$a=$_POST['num1']*$_POST['num2'];
}elseif($_POST['sel']=='/'){
$a=$_POST['num1']/$_POST['num2'];
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<style type="text/css">
table{
width:400px;
height:200px;
margin-left:350px;
margin-top:100px;
background-color:#999999;
text-align:center;
}
</style>
</head>
<body>
<table>
<form method="post" action="">
<tr>
<td>
<input type="number" name="num1" value="<?php if(isset($_POST['num1'])){ echo $_POST['num1']; } ?>"/>
</td>
<td>
<select name="sel">
<option value="+" <?php if(isset($_POST['sel'])){ echo $_POST['sel']="+"?"selected":""; } ?> > +</option>
<option value="-" <?php if(isset($_POST['sel'])) echo $_POST['sel']=="-"?"selected":"";?> >-</option>
<option value="*" <?php if(isset($_POST['sel'])) echo $_POST['sel']=="*"?"selected":"";?> >*</option>
<option value="/" <?php if(isset($_POST['sel'])) echo $_POST['sel']=="/"?"selected":"";?> >/</option>
</select>
</td>
<td>
<input type="number" name="num2" value="<?php if(isset($_POST['num2'])){echo $_POST['num2'];} ?>" />
</td>
<td>
<input type="submit" name="sub" value="="/>
</td>
<td>
<input type="text" name="result" value="<?php if(isset($a)){echo $a;} ?>"/>
</td>
</tr>
</form>
</table>
</body>
</html>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询