<?php
// 定义变量并默认设为空值
$nameErr = $emailErr = $genderErr = $websiteErr = "";
$name = $email = $gender = $comment = $website = ""; if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["name"])) {
$nameErr = "名字是必需的。";
} else {
$name = test_input($_POST["name"]);
} if (empty($_POST["email"])) {
$emailErr = "邮箱是必需的。";
} else {
$email = test_input($_POST["email"]);
} if (empty($_POST["website"])) {
$website = "";
} else {
$website = test_input($_POST["website"]);
} if (empty($_POST["comment"])) {
$comment = "";
} else {
$comment = test_input($_POST["comment"]);
} if (empty($_POST["gender"])) {
$genderErr = "性别是必需的。";
} else {
$gender = test_input($_POST["gender"]);
}
}
?>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>菜鸟教程(runoob.com)</title>
<style>
.error {color: #FF0000;}
</style>
</head>
<body> <?php
// 定义变量并默认设为空值
$nameErr = $emailErr = $genderErr = $websiteErr = "";
$name = $email = $gender = $comment = $website = ""; if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["name"])) {
$nameErr = "名字是必须的。";
} else {
$name = test_input($_POST["name"]);
} if (empty($_POST["email"])) {
$emailErr = "邮箱是必须的。";
} else {
$email = test_input($_POST["email"]);
} if (empty($_POST["website"])) {
$website = "";
} else {
$website = test_input($_POST["website"]);
} 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>
<p><span class="error">* 必填字段。</span></p>
<form method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>">
名字: <input type="text" name="name">
<span class="error">* <?php echo $nameErr;?></span>
<br><br>
E-mail: <input type="text" name="email">
<span class="error">* <?php echo $emailErr;?></span>
<br><br>
网址: <input type="text" name="website">
<span class="error"><?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">* <?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>
</html>

吴裕雄--天生自然 PHP开发学习:表单 - 必需字段的更多相关文章

  1. 吴裕雄--天生自然 PHP开发学习:表单 - 验证邮件和URL

    $name = test_input($_POST["name"]); if (!preg_match("/^[a-zA-Z ]*$/",$name)) { $ ...

  2. 吴裕雄--天生自然 PHP开发学习:表单验证

    <!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title> ...

  3. 吴裕雄--天生自然 JAVASCRIPT开发学习:对象 实例(2)

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  4. 吴裕雄--天生自然 JAVASCRIPT开发学习: 正则表达式

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  5. 吴裕雄--天生自然 PYTHON3开发学习:MySQL - mysql-connector 驱动

    import mysql.connector mydb = mysql.connector.connect( host="localhost", # 数据库主机地址 user=&q ...

  6. 吴裕雄--天生自然 PHP开发学习:连接 MySQL、创建表

    <?php $servername = "localhost"; $username = "root"; $password = "admin& ...

  7. 吴裕雄--天生自然 JAVASCRIPT开发学习: 表单验证

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  8. 吴裕雄--天生自然 JAVASCRIPT开发学习: 表单

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script> ...

  9. 吴裕雄--天生自然 PHP开发学习:表单和用户输入

    <html> <head> <meta charset="utf-8"> <title>菜鸟教程(runoob.com)</t ...

随机推荐

  1. windows平台上运行Flink_转载于CSDN

    Flink安装部署-window 本地部署原创冰上浮云 发布于2019-08-17 15:56:06 阅读数 633 收藏分类专栏: flink版权声明:本文为博主原创文章,遵循 CC 4.0 BY- ...

  2. Java 逆序打印链表

    递归 package cookie; public class PrintListReversal { public void reversalOut(Node head) { if (head != ...

  3. ELK 安装Elasticsearch

    章节 ELK 介绍 ELK 安装Elasticsearch ELK 安装Kibana ELK 安装Beat ELK 安装Logstash ELK栈要安装以下开源组件: Elasticsearch Ki ...

  4. ZOJ - 3961 Let's Chat(区间相交)

    题意:给定一个长度为n的序列,A和B两人分别给定一些按递增顺序排列的区间,区间个数分别为x和y,问被A和B同时给定的区间中长度为m的子区间个数. 分析: 1.1 ≤ n ≤ 109,而1 ≤x, y  ...

  5. SpringAOP 使用注解的简单使用

    1. 导入jar包 /SpringAOPmy/lib/com.springsource.net.sf.cglib-2.2.0.jar/SpringAOPmy/lib/com.springsource. ...

  6. nginx报错

    1. nginx报错 nginx: [error] invalid PID number "" in "/usr/local/nginx/logs/nginx.pid&q ...

  7. div自动适应高度

    div高度100%<!DOCTYPE html> <html> <head></head> <body> <div id=" ...

  8. java基础——深拷贝和浅拷贝的区别

    浅拷贝:对基本数据类型进行值传递,对引用数据类型进行引用传递般的拷贝,此为浅拷贝. 深拷贝:对基本数据类型进行值传递,对引用数据类型船舰一个新的对象,并复制内容,这是深拷贝.

  9. jdk 的安装教程

    1. 配置的位置 鼠标右击计算机----属性-----高级系统设置---环境变量----在系统变量中配置(推荐) 2.增加环境变量 (1)新建 -----  变量名:   JAVA_HOME 变量值 ...

  10. java基础二 分支循环

    分支循环:    if...    if...else...    if...else if...    if...else if...else...    switch...case...defau ...