php-简单计算器

先说一下get和post两种传数方法

get:(URL)用地址传 ?name=xxx&age=xxx

比方说:

<a href="server.php?name=zhangsan&age=10">server</a>

<form action="server.php" method="post">
	username: <input type="text" name="name"> <br>
	age: <input type="text" name="age"> <br>

	<input type="submit" name="sub" value="提交">

<form>	

<?php
/*
echo $_GET['name']."<br>";
echo $_GET['age']."<br>";

 

var_dump($_GET);
echo '<br>';
var_dump($_POST);

 */


	echo $_POST['name'];
	echo $_POST['age'];
	echo $_POST['sub'];

php-简单计算器_第1张图片表示浏览器不会设置 

应该加一行<meta charset="UTF-8"> 的

很少用

post(http)用表单传:用的多 比方说这个计算器:

<?php
	error_reporting(E_ALL & ~E_NOTICE);

	if( isset($_POST['sub']) ) {
		//echo "用户是提交了";
		
		$bz = true;	

		$errormess = "有以上问题:<br>";
		
		if($_POST['num1'] == "") {
			$bz = false;
			$errormess .= "第一个数不能为空<br>";

		} else {
			if(!is_numeric($_POST['num1'])) {
				$bz = false;
				$errormess .="第一个不是数字不能计算<br>";	
			}
		}

		if($_POST['num2'] == "") {
			$bz = false;
			$errormess .= "第二个数不能为空<br>";
		}else {
			if(!is_numeric($_POST['num2'])) {
				$bz = false;
				$errormess .="第二个不是数字不能计算<br>";	
			}

		}





	



		if($bz) {

			//这个是计算后的结果
			$sum = "";
		
			//判断用户选择的是那个运算符号
			switch($_POST['ysf']) {
				case '+': 
					$sum = $_POST['num1'] + $_POST['num2']; 
					break;
				case '-': 
					$sum = $_POST['num1'] - $_POST['num2']; 
					break;
				case 'x': 
					$sum = $_POST['num1'] * $_POST['num2']; 
					break;
				case '/': 
					$sum = $_POST['num1'] / $_POST['num2']; 
					break;
				case '%': 
					$sum = $_POST['num1'] % $_POST['num2']; 
					break;
		
			
			}
		}
	

	} 

echo '<br>';
?>

<html>
	<head>
		<meta charset="UTF-8">  
		<title>简单计算器</title>
	</head>

	<body>
		<table border="0" width="400" align="center">
		<form action="jsq.php" method="post">
			<caption><h1>简单计算器</h1></caption>

			<tr>
				<td> 
					<input type="text" size="5" name="num1" value="<?php echo $_POST['num1'] ?>" /> <br>
				</td>

				<td>
					<select name="ysf">
						<option <?php if($_POST['ysf']=="+") echo "selected" ?> value="+"> + </option>
						<option <?php if($_POST['ysf']=="-") echo "selected" ?>  value="-"> - </option>
						<option <?php echo $_POST['ysf']=="x" ? "selected" : "" ?>  value="x"> x </option>
						<option <?php if($_POST['ysf']=="/") echo "selected" ?>  value="/"> / </option>
						<option <?php if($_POST['ysf']=="%") echo "selected" ?>  value="%"> % </option>
					</select>
				</td>

				<td>

					<input type="text" size="5" name="num2" value="<?php echo $_POST['num2'] ?>" /> <br>
				</td>


				<td>
					<input type="submit" name="sub" value="计算">
				</td>
			<tr>

			<tr>
				<td colspan="4">

					<?php
						if($bz) {

							echo "计算结果: {$_POST['num1']} {$_POST['ysf']} {$_POST['num2']} = {$sum}";
						} else {
						
							echo $errormess;
						}

					?>
				</td>
			</tr>
		</form>
		</table>
	</body>
</html>

php-简单计算器_第2张图片


你可能感兴趣的:(php-简单计算器)