<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>表单验证</title>
<link rel="stylesheet" href="dist/css/bootstrap.css">
<script src="dist/js/jquery-3.2.1.min.js"></script>
<script src="dist/js/bootstrap.js"></script>
<style>
.grid{
border:1px solid #d9211f;
background-color: #fff5ec;
margin-top:30px;
padding-bottom: 50px
}
.bold{
font-weight: bold;
}
.padding{
padding:6px 0px;
}
.error{
color:red;
}
</style>
</head>
<body>
<?php
header("Content-type:text/html; charset=utf-8");
$nameErr = $emailErr = $passErr="";
$username =$password = $email = $gender = $age = "";
function test_input($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
if (empty($_POST["username"]))
{
$nameErr = "用户名不能为空";
}
else
{
$username = test_input($_POST["username"]);
// 检测名字是否只包含字母跟空格
if (!preg_match("/^[a-zA-Z ]*$/",$username))
{
$nameErr = "只允许字母和空格";
}
}
if (empty($_POST["password"]))
{
$passErr = "密码不能为空";
}
else
{
$password = test_input($_POST["password"]);
}
if (empty($_POST["email"]))
{
$emailErr = "邮箱不能为空";
}
else
{
$email = test_input($_POST["email"]);
// 检测邮箱是否合法
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email))
{
$emailErr = "非法邮箱格式";
}
}
if (empty($_POST["gender"]))
{
}
else
{
$temp1 = test_input($_POST["gender"]);
if($temp1 == 'male'){
$gender ='男';
}else{
$gender ='女';
}
}
if (empty($_POST["age"]))
{
}
else
{
$temp2 = test_input($_POST["age"]);
if($temp2 == '1'){
$age ='30以下';
}else if($temp2 == '2'){
$age ='30~60';
}else{
$age = '60以上';
}
}
}
?>
<form method="POST" action='<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>'>
<div class="container grid">
<div>
<div class="col-md-12 text-center ">
<h3>用户注册</h3>
</div>
</div>
<div class="row padding">
<div class="col-md-4 col-md-offset-3">
<label for="username">
用户名:<input type="text" name="username" id="username">
</label>
</div>
<div class="col-md-3 ">
<label for="tipusername">
*<?php echo $nameErr;?>
</label>
</div>
</div>
<div class="row padding">
<div class="col-md-4 col-md-offset-3">
<label for="password">
密 码:<input type="password" name="password" id="password">
</label>
</div>
<div class="col-md-3 ">
<label for="tipusername">
*<?php echo $passErr;?>
</label>
</div>
</div>
<div class="row padding">
<div class="col-md-4 col-md-offset-3">
<label for="email">
邮 箱:<input type="text" name="email" id="email">
</label>
</div>
<div class="col-md-3 ">
<label for="tipusername">
*<?php echo $emailErr;?>
</label>
</div>
</div>
<div class="row padding">
<div class="col-md-4 col-md-offset-3 bold">
性 别:
<input type="radio" name="gender" value="male">男
<input type="radio" name="gender" value="female">女
</div>
</div>
<div class="row padding">
<div class="col-md-4 col-md-offset-3 bold">
年 龄:
<select name="age" id="age">
<option value="1">30以下</option>
<option value="1">30~60</option>
<option value="1">60以上</option>
</select>
</div>
</div>
<div class="row padding">
<div class="col-md-4 col-md-offset-3">
<input id="sub" type="submit" name="submit" value="提交">
</div>
</div>
</div>
</form>
<div>
<div>
<div>
<?php
echo "<h3>您输入的内容是:</h3>";
echo '用户名:'.$username;
echo "<br>";
echo '邮箱:'.$email;
echo "<br>";
echo '性别:'.$gender;
echo "<br>";
echo '年龄:'.$age;
echo "<br>";
?>
</div>
</div>
</div>
</body>
</html>
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号