1. 程式人生 > >php+html實現網頁計算器

php+html實現網頁計算器

ech border 一行 select color else 輸出數據 -c col

<!--設置表格內居中對齊,表格邊框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>

完全代碼

php+html實現網頁計算器