<!--设置表格内居中对齐,表格边框2px-->
<table align="center" border="2px"> <tr> <td> <input type="text" name="number1" size=4> </td> <td> <select name="caculate" size="1"> <option value="+">+</option> <option value="-">-</option> <option value="*">*</option> <option value="/">/</option> <option value="%">%</option> </select> </td> <td> <input type="text" name="number2" size=4> </td> <td> <input type="submit" name="ok" value="计算" size=4> </td> </tr>
设置第一行(输入行)
<tr align="center" >
<!--colspan用来合并单元格,使输出数据居中,更美观。--> <td width="full" colspan="4"> <?php function caculate($numb1,$numb2,$method) { switch($method) { case \'+\': return $numb1+$numb2; break; case \'-\': return $numb1-$numb2; break; case \'*\': return $numb1*$numb2; break; case \'/\': return $numb1/$numb2; break; case \'%\': return $numb1%$numb2; break; default: break; } } if(isset($_POST[\'ok\'])) { $numb1=$_POST[\'number1\']; $numb2=$_POST[\'number2\']; $method=$_POST[\'caculate\']; if(is_numeric($numb1)&&is_numeric($numb2)) { if($method==\'/\'&&$numb2==0) { echo "<script>alert(\'除数不能为“0”,请重新输入!\')</script>"; return; } $res=caculate($numb1,$numb2,$method); echo "$numb1 $method $numb2 = $res "; } else { echo "<script>alert(\'您输入的不是数字,请重新输入!\')</script>"; } } ?> </td> </tr>
设置第二行(输出行)
1 <!doctype html> 2 <html> 3 <head> 4 <meta charset="utf-8"> 5 <title>网页计算机</title> 6 </head> 7 <style type= "text/css"> 8 h1{ 9 font-size=24px; 10 text-align: center; 11 font-family: "黑体" ; 12 } 13 14 15 </style> 16 <body> 17 <h1>网页计算器</h1> 18 <form method="post"> 19 <table align="center" border="2px"> 20 <tr> 21 <td> 22 <input type="text" name="number1" size=4> 23 </td> 24 <td> 25 <select name="caculate" size="1"> 26 <option value="+">+</option> 27 <option value="-">-</option> 28 <option value="*">*</option> 29 <option value="/">/</option> 30 <option value="%">%</option> 31 </select> 32 </td> 33 <td> 34 <input type="text" name="number2" size=4> 35 </td> 36 <td> 37 <input type="submit" name="ok" value="计算" size=4> 38 </td> 39 </tr> 40 <tr align="center" > 41 <td width="full" colspan="4"> 42 <?php 43 function caculate($numb1,$numb2,$method) 44 { 45 switch($method) 46 { 47 case \'+\': 48 return $numb1+$numb2; 49 break; 50 case \'-\': 51 return $numb1-$numb2; 52 break; 53 case \'*\': 54 return $numb1*$numb2; 55 break; 56 case \'/\': 57 return $numb1/$numb2; 58 break; 59 case \'%\': 60 return $numb1%$numb2; 61 break; 62 default: 63 break; 64 } 65 } 66 if(isset($_POST[\'ok\'])) 67 { 68 $numb1=$_POST[\'number1\']; 69 $numb2=$_POST[\'number2\']; 70 $method=$_POST[\'caculate\']; 71 if(is_numeric($numb1)&&is_numeric($numb2)) 72 { 73 if($method==\'/\'&&$numb2==0) 74 { 75 echo "<script>alert(\'除数不能为“0”,请重新输入!\')</script>"; 76 return; 77 } 78 $res=caculate($numb1,$numb2,$method); 79 echo "$numb1 $method $numb2 = $res "; 80 } 81 else 82 { 83 echo "<script>alert(\'您输入的不是数字,请重新输入!\')</script>"; 84 } 85 } 86 ?> 87 </td> 88 </tr> 89 </table> 90 </form> 91 92 </body> 93 </html>
完全代码