表单
表单提交
form.html
<html>
<head>
<meta charset="utf-8">
<title>表单提交测试</title>
</head>
<body>
<form action="form_action.php" method="post">
名字: <input type="text" name="fname">
年龄: <input type="text" name="age">
<input type="submit" value="提交">
</form>
</body>
</html>
form_action.php
姓名: <?php echo $_POST["fname"]; ?>!<br>
年龄: <?php echo $_POST["age"]; ?>
下拉框&选择狂
<?php
//获取get请求方式的参数,不能获取多选框的数组
//$q = isset($_GET['q'])? htmlspecialchars($_GET['q']) : '';
//获取post请求方式的参数,多选只能用这种
$q = isset($_POST['q'])? $_POST['q'] : '';
if(is_array($q)) { //多选
$sites = array(
'apple' => '苹果',
'banana' => '香蕉',
'orange' => '橘子',
);
foreach ($q as $val) {
// PHP_EOL 为常量,用于换行
echo $sites[$val] . PHP_EOL;
}
}
elseif ($q) { //单选
if($q =='apple') {
echo '选择了苹果<br>';
} else if($q =='banana') {
echo '选择了香蕉<br>';
} else if($q =='orange') {
echo '选择了橘子<br>';
}
} else {
?>
<!--下拉单选-->
<form action="" method="post">
<select name="q">
<option value="">请选择:</option>
<option value="apple">苹果</option>
<option value="banana">香蕉</option>
<option value="orange">橘子</option>
</select>
<input type="submit" value="提交">
</form>
<!--下拉多选-->
<form action="" method="post">
<select multiple="multiple" name="q[]>
<option value="">请选择:</option>
<option value="apple">苹果</option>
<option value="banana">香蕉</option>
<option value="orange">橘子</option>
</select>
<input type="submit" value="提交">
</form>
<!--radio单选-->
<form action="" method="post">
请选择:
<input type="radio" name="q" value="apple"/>苹果
<input type="radio" name="q" value="banana"/>香蕉
<input type="radio" name="q" value="orange"/>橘子
<input type="submit" value="提交">
</form>
<!--checkbox单选-->
<form action="" method="post">
请选择:
<input type="checkbox" name="q[]" value="apple"/>苹果
<input type="checkbox" name="q[]" value="banana"/>香蕉
<input type="checkbox" name="q[]" value="orange"/>橘子
<input type="submit" value="提交">
</form>
<?php
}
?>
表单数据验证
必填验证、数据格式验证、
<?php
// 定义变量并默认设置为空值
$nameErr = $emailErr = $genderErr = $websiteErr = "";
$name = $email = $gender = $comment = $website = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["name"])) {
$nameErr = "Name is required";
} else {
$name = test_input($_POST["name"]);
// 检测名字是否只包含字母跟空格
if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
$nameErr = "只允许字母和空格";
}
}
if (empty($_POST["email"])) {
$emailErr = "Email is required";
} else {
$email = test_input($_POST["email"]);
// 检测邮箱是否合法
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email)) {
$emailErr = "非法邮箱格式";
}
}
if (empty($_POST["website"])) {
$website = "";
} else {
$website = test_input($_POST["website"]);
// 检测 URL 地址是否合法
if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website)) {
$websiteErr = "非法的 URL 的地址";
}
}
if (empty($_POST["comment"])) {
$comment = "";
} else {
$comment = test_input($_POST["comment"]);
}
if (empty($_POST["gender"])) {
$genderErr = "性别是必需的";
} else {
$gender = test_input($_POST["gender"]);
}
}
//校验数据方法
function test_input($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<h2>PHP 表单验证</h2>
<!--$_SERVER["PHP_SELF"]为本身路径,htmlspecialchars为了防止跨域攻击-->
<form method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>">
名字: <input type="text" name="name">
<span class="error" style="color: #FF0000;">* <?php echo $nameErr;?></span>
<br><br>
E-mail: <input type="text" name="email">
<span class="error" style="color: #FF0000;">* <?php echo $emailErr;?></span>
<br><br>
网址: <input type="text" name="website">
<span class="error" style="color: #FF0000;"><?php echo $websiteErr;?></span>
<br><br>
备注: <textarea name="comment" rows="5" cols="40"></textarea>
<br><br>
性别:
<input type="radio" name="gender" value="female">女
<input type="radio" name="gender" value="male">男
<span class="error" style="color: #FF0000;">* <?php echo $genderErr;?></span>
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
<?php
echo "<h2>您输入的内容是:</h2>";
echo $name;
echo "<br>";
echo $email;
echo "<br>";
echo $website;
echo "<br>";
echo $comment;
echo "<br>";
echo $gender;
?>
</body>
$_REQUEST 变量
预定义的 $_REQUEST 变量包含了 $_GET、$_POST 和 $_COOKIE 的内容。
$_REQUEST 变量可用来收集通过 GET 和 POST 方法发送的表单数据,如果不确定是get还是post请求可以用$_REQUEST获取参数。