Calculator.php:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<link rel="stylesheet" type="text/css" href="calculatpr.css" >
<title>计算器</title>
</head>
<body>
<div class="style1"></br>
<div class="style2"><b class="style2" >我的计算器</b></div><br>
//当提交页面为当前页面时,可用以下语句,比直接用当前文件名安全
//<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
//除了以上方法,还可以用当前PHP文件名或action="" 也可
<form action="result.php" method="post">
<!--action 指定了目标文件名称,method制定了数据传输方法,由get和post两种方法-->
num1:
<input type="text" name="num1" />
<br /><br />
num2:
<input type="text" name="num2" />
<br/><br />
运算符:
<select class="style4" name="operator">
<option value="加法">+</option>
<option value="减法">-</option>
<option value="乘法">*</option>
<option value="除法">/</option>
</select>
<br/><br/>
<input type="submit" value="计算" /><br>
<!--注意:一个提交按钮(type="submit")只能提交按钮所在的表单(<form>)中的数据,不能提交替他数据-->
</form>
</div>
</body>
</html>
result.php:
<?php
/*
对于数据的接收有3种方法:
若原文件数据提交由get方法,可使用$_GET[]方法接收数据;
若原文件数据提交由post方法,可使用$_POST[]方法接收数据;
还有一种通用方法$_REQUEST[],无论原文件用get还是post方法提交数据,都可用该方法接收。
*/
$number1 = $_REQUEST['num1']; /* 中括号中数据为所要接收数据的name属性的值 */
$number2 = $_REQUEST['num2'];
$operator = $_REQUEST['operator'];
echo $number1;
echo "<br>";
echo $number2;
echo "<br>";
echo $operator;
echo "<br>";
if($operator == "加法" )
{
$result=$number1 + $number2;
}
else if($operator == "减法" )
{
$result=$number1 - $number2;
}
else if($operator == "乘法" )
{
$result=$number1 * $number2;
}
else
{
if($number2 == 0 ){
$result = "除数不能为零!";
}
else{
$result=$number1 / $number2;
}
}
echo $result;
echo "<br>";
echo "<br>";
?>
<a href = "Calculator.php">返回计算界面</a>
calculatpr.css:
.style1{
width:250px;
height:350px;
padding:5px;
/* background:red; */
border-style: solid;
border-width: 2px;
align:center;
}
.style2{
text-align:center;
}