php页面切换、表单数据提交代码(计算器


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;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值