php+html 實現加減乘除
阿新 • • 發佈:2019-02-04
首先建立一個.php的文件
<HTML> <HEAD> <TITLE>表單資料傳遞</TITLE> </HEAD> <BODY> <?php if (isset($_POST["tag"])){ $addend1=$_POST["addend1"]; $addend2=$_POST["addend2"]; $method =$_POST['method']; }else{ $addend1=0; $addend2=0; $method ='+'; } switch ($method){ case '+': $result = $addend1 + $addend2; break; case '-': $result = $addend1 - $addend2; break; case 'x': $result = $addend1 * $addend2; break; case '/': $addend2 = $addend2 == 0? 1: $addend2; $result = $addend1 / $addend2; break; case '%': $addend2 = $addend2 == 0? 1: $addend2; $result = $addend1 % $addend2; break; default: $method ='+'; $result = $addend1 + $addend2; } ?> <meta charset="utf-8"> <form name="form1" method="post" action="#"> <!--下面是一個隱藏表單,接收後用以來判斷是提交前的頁面還是提交後的頁面--> <input type="hidden" name="tag" size="4" value="1"> <input type="text" name="addend1" size="4" value="<?php echo $addend1;?>"> <select name="method"> <option value='+'<?php if($method=='+')echo " selected='selected'";?>>+</option> <option value='-'<?php if($method=='-')echo " selected='selected'";?>>-</option> <option value='x'<?php if($method=='x')echo " selected='selected'";?>>x</option> <option value='/'<?php if($method=='/')echo " selected='selected'";?>>/</option> <option value='%'<?php if($method=='%')echo " selected='selected'";?>>%</option> </select> <input type="text" name="addend2" size="4" value="<?php echo $addend2;?>"> = <?php echo $result;?><br> <br><input type="submit" name="button1" value="計算"> </form> </BODY> </HTML>