基于php基础语言编写的小程序之计算器
需求:在输入框中输入数字进行加、减、乘、除运算(html+php)
思路:
1首先要创建输入数字和运算符的输入框,数字用input的text属性,运算符用selelct的option属性
2 点击输入框中的=号要进行对应的运算,
3 =号这个输入框可以用input的submit来做,只要点击submit表单里的内容就传给php了
4 判断从html中得到的运算符进行对应的运算
5 运算完成后还得把结果返回到表单中(就是给表单的value赋值)
代码
Html代码
1 2 3 4 5 6 7 8 9 10 11 12 | <form method="post" action=””>
<input type = "text" name="num1" >
<select name = "select">
<option value="+" >+</option>
<option value="-" >-</option>
<option value="*" >*</option>
<option value="/" >/</option>
</select>
<input type = "text" name="num2" >
<input type = "submit" name = "submit" value="=">
<input type = "text" name="result" >
</form>
|
Salin selepas log masuk

PHP代码
当用户点击提交按钮值就会通过post传递过来,现在要接受表单里的值。
在点击之前要做几个判断
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | if (isset( $_POST ['submit'])) {
$num1 = $_POST ['num1'];
$select = $_POST ['select'];
$num2 = $_POST ['num2'];
if ( is_numeric ( $num1 ) && is_numeric ( $num2 )) {
switch ( $select ) {
case '+':
$result = $num1 + $num2 ;
break ;
case '-':
$result = $num1 - $num2 ;
break ;
case '*':
$result = $num1 * $num2 ;
break ;
default :
if ( $num2 ==0) {
echo "<script>alert('输入的除数为0请重新输入')</script>";
} else {
$result = $num1 / $num2 ;
break ;
}
}
} else {
echo "<script>alert('输入的不是数')</script>";
$num1 = $num2 = $result = "";
}
}
|
Salin selepas log masuk
运行结果截图
当输入正确的数字截图

点击=号后

说明值没有传给html中的表单,
现在要去设置表单的value
<input type = "text" name="num1" value="<?php echo $num1?>" >//把value的值设置为php中运算后的num值
<select name = "select">
<option value="+" >+</option>
<option value="-" >-</option>
<option value="*" >*</option>
<option value="/" >/</option>
</select>
<input type = "text" name="num2" value="<?php echo $num2?>" >
<input type = "submit" name = "submit" value="=">
<input type = "text" name="result" value="<?php echo $result?>">
</form>
运行结果

在用户没有点击提交按钮时输入框现在有内容,所以在用户没有点击提交按钮时应该把输入框中的值置为空
改进带码,在php的代码最后加一个else{
$num1 =$num2 = $result = "";
}
截图

在点击其他运算时,中间的运算符始终为+,截图

代码改进
在html中
<select name = "select">
<option value="+" <?php if($select == '+')echo 'selected'?>>+</option>
//select有一个属性selected当设置了就默认选中了它所以得结合php传过来的值比较,true就代表选中false就代表未选
<option value="-" <?php if($select == '-')echo 'selected'?>>-</option>
<option value="*" <?php if($select == '*')echo 'selected'?>>*</option>
<option value="/" <?php if($select == '/')echo 'selected'?>>/</option>
</select>
截图看结果

当用户第一次进来
截图

说明要设置selecte中的默认值
代码
$select=”+”
基本功能已经完成
总的代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | <!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<?php
if (isset( $_POST ['submit'])) {
$num1 = $_POST ['num1'];
$select = $_POST ['select'];
$num2 = $_POST ['num2'];
if ( is_numeric ( $num1 ) && is_numeric ( $num2 )) {
switch ( $select ) {
case '+':
$result = $num1 + $num2 ;
break ;
case '-':
$result = $num1 - $num2 ;
break ;
case '*':
$result = $num1 * $num2 ;
break ;
default :
if ( $num2 ==0) {
echo "<script>alert('输入的除数为0请重新输入')</script>";
} else {
$result = $num1 / $num2 ;
break ;
}
}
} else {
echo "<script>alert('输入的不是数')</script>";
$num1 = $num2 = $result = "";
}
} else {
$num1 = $num2 = $result = "";
$select = "+";
}
?>
<form method="post" action=""><!--
<input type = "text" name="num1" value="<?php echo $num1 ?>" >
<select name = "select">
<option value="+" <?php if ( $select == '+') echo 'selected'?>>+</option>
<option value="-" <?php if ( $select == '-') echo 'selected'?>>-</option>
<option value="*" <?php if ( $select == '*') echo 'selected'?>>*</option>
<option value="/" <?php if ( $select == '/') echo 'selected'?>>/</option>
</select>
<input type = "text" name="num2" value="<?php echo $num2 ?>" >
<input type = "submit" name = "submit" value="=">
<input type = "text" name="result" value="<?php echo $result ?>">
</form>
</body>
</html>
|
Salin selepas log masuk