<form> <input type="text" name="num1"> <select name="fh"> <option value="jia">+</option> <option value="jian">-</option> <option value="chen" >*</option> <option value="chu">/</option> <option value="quzhi">%</option> </select> <input type="text" name="num2"> <input type="button" value="提交"> </form> <?php $num1=$_GET["num1"]; $num2=$_GET["num2"]; $a=$_GET['fh']; if(!is_numeric($num1)||!is_numeric($num2)){ echo "请输入数字"; } if($a=="jia"){ echo $num1.'+'.$num2.'='.($num1+$num2); } if($a=="jian"){ echo $num1.'-'.$num2.'='.($num1-$num2); } if($a=="chen"){ echo $num1.'*'.$num2.'='.($num1*$num2); } if($a=="chu"){ echo $num1.'/'.$num2.'='.($num1/$num2); } if($a=="quzhi"){ echo $num1.'%'.$num2.'='.($num1%$num2); } ?>
如題
寻觅 beyond2017-10-13 20:42:17
<form action='' method="get"> <input type="text" name="num1"> <select name="fh"> <option value="jia">+</option> <option value="jian">-</option> <option value="chen" >*</option> <option value="chu">/</option> <option value="quzhi">%</option> </select> <input type="text" name="num2"> <!-- <input type="button" value="提交"> --> <input type="submit" name="submit" value="提交"> </form> <?php if(isset($_GET['submit'])){ $num1=$_GET["num1"]; $num2=$_GET["num2"]; $a=$_GET['fh']; if(!is_numeric($num1)||!is_numeric($num2)){ echo "请输入数字"; } if($a=="jia"){ echo $num1.'+'.$num2.'='.($num1+$num2); } if($a=="jian"){ echo $num1.'-'.$num2.'='.($num1-$num2); } if($a=="chen"){ echo $num1.'*'.$num2.'='.($num1*$num2); } if($a=="chu"){ echo $num1.'/'.$num2.'='.($num1/$num2); } if($a=="quzhi"){ echo $num1.'%'.$num2.'='.($num1%$num2); } } ?>
程式碼我幫你改了一下,首先提交時類型為submit而不是用button,而且你的表單的action和method最好顯式寫出來,雖然不寫是用get方式默認傳到本頁面;然後,php程式碼最好先判斷是否接收到提交,如果不判斷是否接收到提交,最開始會有警告