<html>
<head>
<title>
<?php
echo "我的計算器";
?>
</title>
</head>
<script type="text/javascript">
function Onclick()
{
var num1 = document.getElementById('num1').value;
var num2 = document.getElementById('num2').value;
if(num1=="")
{
alert("num1輸入不正確");
}
if(num2=="")
{
alert("num2輸入不正確");
}
if(num1==""||num2=="")
{
return false;
}
return true;
}
</script>
<body>
<center>我的計算器<br/>
<form action="index.php" method="post">
num1:<input type="text" id="num1" name="num1" value=""><br/>
num2:<input type="text" name="num2" value=""><br/>
計算方式:<select name="run">
<option value="+">加</option>
<option value="-">減</option>
<option value="*">乘</option>
<option value="/">除</option>
</select>
<input type="submit" name="submit"value="計算" onClick="return Onclick();"/><br/>
</form>
<?php
$num1 = $_POST['num1'];
$num2 =$_REQUEST['num2'];
$run = $_REQUEST['run'];
switch($run)
{
case '+':print("兩數之和為:".($num1+$num2));break;
case '-':print("兩數之差為:".($num1-$num2));break;
case '*':print("兩數之乘積為:".($num1*$num2));break;
case '/':print("兩數之和為:".($num1/$num2));break;
}
?>
</center>
</body>
</html>
本文出自 “ITeamsky-楊波的技術空間” 博客