博客列表 >PHP表单验证

PHP表单验证

Pengsir
Pengsir原创
2017年12月27日 11:01:09639浏览

主页代码:

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
 content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>PHP表单验证</title>
    <style>
        body{
            margin: 0;
            padding: 0;
            background-color:powderblue;
        }
        .box{
            width: 600px;
            height: 330px;
            margin: 100px auto;
            padding-top: 10px;
            background-color: white;
        }
        .reg{
            color: red;
            font-size: 1.5em;
        }
        input{
            background: #eeeeee0f;
            border-radius: 5px;
            padding-left: 5px;
            border: 1px solid #3a6c7e;
            font-size: 100%;
            outline: 0;
            font-family: 微软雅黑;
            line-height: 1.5;
        }
        #on{
            display: block;
            width: 30%;
            height: 40px;
            background-color: sandybrown;
            border-radius: 5px;
            font-size: 16px;
            color: #fff;
            text-align: center;
            line-height: 40px;
        }
        #select {
            font-size: 100%;
            border-radius: 5px;
            width: 20%;
            background: #eeeeee0f;
            border-radius: 5px;
            padding-left: 5px;
            border: 1px solid #3a6c7e;


    </style>
</head>
<body>
<div class="box">
<form action="check.php" method="post">
    <fieldset>
        <legend class="reg">注册</legend>
        <p>
            <labal>用户:<input type="text" name="name"></labal>
        </p>
        <p>
            <labal>邮箱:<input type="text" name="email"></labal>
        </p>
        <p>性别:
            <input type="radio" name="gender" value="man">帅哥
            <input type="radio" name="gender" value="woman">美女
            <input type="radio" name="gender" value="shemale">待定
        </p>
        <p>
            <label>年龄:
                <select id="select" name="age">
                    <option value="1">30以内</option>
                    <option value="2">30到60</option>
                    <option value="3">60以上</option>
                </select>
            </label>
        </p>
        <p>备注:
            <textarea name="comments" id="" cols="30" rows="5"></textarea>
        </p>
    </fieldset>
   <p align="center"> <input id="on" type="submit" name="submit" value="提交"></p>
</form>
</div>
</body>
</html>

验证php代码

<?php
header('Content-type:text/html;Charset=utf-8');
//1.先检查变量name(用户名)存不存在,存在返回name,否则返回null
$name=isset($_REQUEST['name'])? $_REQUEST['name'] :null;
//2.再判断用户名是否为空
if(empty($name)){
    echo '<script>alert("您没有输入用户名")</script>>';
}else{
    echo '<script>alert("您的用户名是'.$name.'");location.back()</script>>';
}

$email=isset($_REQUEST['email'])? $_REQUEST['email'] :null;
if(empty($email)){
    echo '<script>alert("您没有输入邮箱")</script>>';
}else{
    echo '<script>alert("您的邮箱是'.$email.'");location.back()</script>>';
}

$gender=isset($_REQUEST['gender'])? $_REQUEST['gender']:null;
switch ($gender){
    case 'man':
       echo '<script>alert("我是大帅哥")</script>>';
        break;
    case 'woman':
        echo '<script>alert("我是来你们的女神")</script>>';
        break;
        case 'shemale':
       echo '<script>alert("我来自泰国")</script>>';
       break;
       default:
           echo '<script>alert("我可男可女,人送外号:大屌萌妹")</script>>';

}

$age=$_REQUEST['age']? $_REQUEST['age']:null;
switch ($age){
    case '1':
       echo '<script>alert("我30以内")</script>>';
        break;
    case '2':
        echo '<script>alert("我年龄30到60之间")</script>>';
        break;
    case '3':
        echo '<script>alert("我年龄60以上")</script>>';
        break;
    default:
        echo '<script>alert("知道我多大了吗?")</script>>';
}

$comments=isset($_REQUEST['comments'])? $_REQUEST['comments']:null;
if(empty($comments)){
    echo '<script>alert("你还没有编辑内容哦")</script>>';
}else{
    echo '<script>alert("你编辑的文本内容是:'.$comments.'")</script>>';
}


上一条:表格生成下一条:PHP表单验证
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议