技术人生,  网络编程

简述PHP_SELF参数

本文简述PHP_SELF参数的含义,并运用该参数将具有多个功能的php程序合并到一个文件中,这是一种常用的方法。

变量 $_SERVER[‘PHP_SELF’] 包含了当前php文件的相对访问路径,在form表单中将action参数设置为它,可以实现对当前php文件提交申请。结合if判断即可组合多功能到同一个php文件中。使用PHP_SELF的好处是不用担心修改php文件名导致程序无法正常运行,从而使代码始终保持通用性。

if 判断条件中可以使用 isset() 函数判断任意一个表单项是否存在于 $_GET 或$_POST数组中,如果存在表示用户已经提交了请求,否则表示用户初次访问该页面。以此实现同一php文件中不同功能的流量控制。

以下程序组合了多个功能,首页为功能选择页,每个功能还需要实现表单输入页和最终结果页。因此使用两层嵌套判断用户当前所在页面。

我还为表单添加了onsubmit属性,其中使用自定义函数beforeSubmit()判断用户的表单输入是否为空,若有任意项目为空则告警并返回false,表单不会继续执行action操作。

<!DOCTYPE html>
<html>
<head>
	<title>Combinational Function</title>
	<script type="text/javascript">
		function beforeSubmit()
		{
			var input_boxes = document.getElementsByTagName("input");
			for (var b = 0; b < input_boxes.length; b++)
			{
				if (! input_boxes[b].value)
				{
					alert("User input cannot be empty.")
					return false;
				}
			}
			return true;
		}
	</script>
</head>
<body>
	<?php
		if (isset($_GET['mode'])) 
		{
			$mode = $_GET['mode'];
			switch ($mode)
			{
				case 'temp':
					echo '<h1>Temperature Conversion Page</h1>';
					if (isset($_GET['temp']))
					{
						$c = $_GET['temp'];
						$f = ($c * 9 / 5) + 32;
						echo ($c . " degrees Celsius is " . $f . " degrees Fahrenheit");
					}
					else
					{
						echo '	<p>Please enter a temperature in Celsius to convert to Fahrenheit</p>
								<form action="' . $_SERVER['PHP_SELF'] . '" onSubmit="return beforeSubmit();">
									<input type="hidden" name="mode" value="temp">
									<input type="text" name="temp" id="temp" />
									<input type="submit" value="Convert" />
								</form>';
					}
					break;
				case 'circle':
					echo '<h1>Calculate Circle</h1>';
					if (isset($_GET['radius']))
					{
						$r = $_GET['radius'];
						$s = pi() * pow($r, 2);
						$c = 2 * pi() * $r;
						echo ('Radius of the circle: ' . $r . '<br>');
						echo ('Area: ' . round($s, 2) . '<br>');
						echo ('Circumference: ' . round($c, 2));
					}
					else
					{
						echo '	<p>Please enter the radius of a circle</p>
								<form action="' . $_SERVER['PHP_SELF'] . '" onSubmit="return beforeSubmit();">
									<input type="hidden" name="mode" value="circle">
									<input type="text" name="radius" />
									<input type="submit" value="Calculate" />
								</form>';
					}
					break;
				case 'sphere':
					echo '<h1>Calculate Sphere</h1>';
					if (isset($_GET['radius']))
					{
						$r = $_GET['radius'];
						$s = 4 * pi() * pow($r, 2);
						echo ('Radius of the sphere: ' . $r . '<br>');
						echo ('Surface area: ' . round($s, 2) . '<br>');
					}
					else
					{
						echo '	<p>Please enter the radius of a Sphere</p>
								<form action="' . $_SERVER['PHP_SELF'] . '" onSubmit="return beforeSubmit();">
									<input type="hidden" name="mode" value="sphere">
									<input type="text" name="radius" />
									<input type="submit" value="Calculate" />
								</form>';
					}
					break;
				case 'cylinder':
					echo '<h1>Calculate Cylinder</h1>';
					if (isset($_GET['radius']))
					{
						$r = $_GET['radius'];
						$h = $_GET['height'];
						$s = 2 * pi() * pow($r, 2) + 2 * pi() * $r * $h;
						echo ('The surface area of a cylinder with height ' . $h . ' and radius ' . $r . ' is ' . round($s, 2) . '.');
					}
					else
					{
						echo '	<form action="' . $_SERVER['PHP_SELF'] . '" onSubmit="return beforeSubmit();">
									<input type="hidden" name="mode" value="cylinder">
									The height:
									<input type="text" name="height" /><br>
									The radius:
									<input type="text" name="radius" /><br>
									<input type="submit" value="Calculate" />
								</form>';
					}
					break;
				case 'initials':
					echo '<h1>Calculate Name</h1>';
					if (isset($_GET['firstN']))
					{
						$first_name = $_GET['firstN'];
						$surname = $_GET['surN'];
						echo ('Your initials: ' . strtoupper(substr($first_name,0,1)) . '.' . strtoupper(substr($surname,0,1)) . '.');
					}
					else
					{
						echo '	<form action="' . $_SERVER['PHP_SELF'] . '" onSubmit="return beforeSubmit();">
									<input type="hidden" name="mode" value="initials">
									First Name:
									<input type="text" name="firstN" /><br>
									Surname:
									<input type="text" name="surN" /><br>
									<input type="submit" value="Calculate" />
								</form>';
					}
					break;
				case 'age':
					echo '<h1>Judge Age</h1>';
					if (isset($_GET['age']))
					{
						$age = $_GET['age'];
						echo ($age > 17);
					}
					else
					{
						echo '	<p>Please enter your age</p>
								<form action="' . $_SERVER['PHP_SELF'] . '" onSubmit="return beforeSubmit();">
									<input type="hidden" name="mode" value="age">
									<input type="text" name="age" />
									<input type="submit" value="Judge" />
								</form>';
					}
					break;
				default:
					echo '* Error: Unexpected argument.';
					break;
			}
		}
		else
		{
			echo '	<h1>Combinational Function</h1>
					<form action="' . $_SERVER['PHP_SELF'] . '">
						What would you want to do?
						<br><br>
						<select name="mode">
							<option value="temp">Convert Temperature</option>
							<option value="circle">Calculate Circle</option>
							<option value="sphere">Calculate Sphere</option>
							<option value="cylinder">Calculate Cylinder</option>
							<option value="initials">Get Name Initials</option>
							<option value="age">Judge Age</option>
						</select>
						<input type="submit" value="Do it!">
					</form>';
		}
	?>
</body>
</html>

A WindRunner. VoyagingOne

留言

您的邮箱地址不会被公开。 必填项已用 * 标注