Blogger Information
Blog 22
fans 0
comment 0
visits 17745
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
php表单验证
yestrue的博客
Original
911 people have browsed it
<!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">
密&nbsp;&nbsp;&nbsp;码:<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">
邮&nbsp;&nbsp;&nbsp;箱:<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">
性&nbsp;&nbsp;&nbsp;别:
<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">
年&nbsp;&nbsp;&nbsp;龄:
<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>


Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post