Blogger Information
Blog 12
fans 0
comment 0
visits 12787
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP中表单的验证
留情的博客
Original
757 people have browsed it

先上待验证表单

<!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>
</head>
<body>
<form action="check.php" method="post">
    <fieldset>
        <legend>注册</legend>
        <p><label>用户:<input type="text" name="name"></label></p>
        <p><label>邮箱:<input type="text" name="email"></label></p>
        <p>性别:
 <label><input type="radio" name="gender" value="male">男</label>
            <label><input type="radio" name="gender" value="female">女</label>
        </p>
        <p>
            <label>
 年龄:
 <select name="age" id="">
                    <option value="1">30以下</option>
                    <option value="2">30以上</option>
                </select>
            </label>
        </p>
        <p>
 备注:
 <textarea name="comments" id="" cols="30" rows="10"></textarea>
        </p>
    </fieldset>
    <input type="submit">
</form>
</body>
</html>

然后送上验证代码

<?php
header('Content-type:text/html;charset=utf-8');
$name = isset($_REQUEST['name']) ? $_REQUEST['name'] : null;
if (empty($name)) {
    echo '<script>alert("请输入用户名");window.history.go(-1)</script>';
} else {
    echo '<script>alert("您的用户名是' . $name . '")</script>';
}
$email = isset($_REQUEST['email']) ? $_REQUEST['email'] : null;
if (empty($email)) {
    echo '<script>alert("请输入邮箱");window.history.go(-1)</script>';
} else {
    echo '<script>alert("您的邮箱是' . $email . '")</script>';
}
$gender = isset($_REQUEST['gender']) ? $_REQUEST['gender'] : null;
switch ($gender) {
    case 'male':
        echo "<script>alert('公的')</script>";
        break;
    case 'female':
        echo '<script>alert("公的")</script>';
        break;
}
$age = isset($_REQUEST['age']) ? $_REQUEST['age'] : null;
switch ($age) {
    case '1':
        echo '<script>alert("挺年轻的啊")</script>';
        break;
    case '2':
        echo '<script>alert("年纪挺大了吧")</script>';
        break;
}
$comments = isset($_REQUEST['comments']) ? $_REQUEST['comments']:null;
if (empty($comments)) {
    echo '<script>alert("你就没什么想说的嘛")</script>';
} else {
    echo '<script>alert("'.$comments.'")</script>';
}


一些备注:

<script>alert("请输入用户名");window.history.go(-1)</script>

javascript中是用分号 ; 来分隔两段语句的。

$name = isset($_REQUEST['name']) ? $_REQUEST['name'] : null;

这是一个三元运算。isset($_REQUEST['name']) 代表待验证表单中name=name这个值是否设定,如果设定了则返回这个值,就是$_REQUEST['name'] 如果为空或者没有设置则返回null

其完整语法为:条件表达式?表达式1:表达式2

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