吴裕雄--天生自然 PHP开发学习:表单 - 必需字段
<?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开发学习:表单 - 必需字段的更多相关文章
- 吴裕雄--天生自然 PHP开发学习:表单 - 验证邮件和URL
$name = test_input($_POST["name"]); if (!preg_match("/^[a-zA-Z ]*$/",$name)) { $ ...
- 吴裕雄--天生自然 PHP开发学习:表单验证
<!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title> ...
- 吴裕雄--天生自然 JAVASCRIPT开发学习:对象 实例(2)
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- 吴裕雄--天生自然 JAVASCRIPT开发学习: 正则表达式
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- 吴裕雄--天生自然 PYTHON3开发学习:MySQL - mysql-connector 驱动
import mysql.connector mydb = mysql.connector.connect( host="localhost", # 数据库主机地址 user=&q ...
- 吴裕雄--天生自然 PHP开发学习:连接 MySQL、创建表
<?php $servername = "localhost"; $username = "root"; $password = "admin& ...
- 吴裕雄--天生自然 JAVASCRIPT开发学习: 表单验证
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- 吴裕雄--天生自然 JAVASCRIPT开发学习: 表单
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script> ...
- 吴裕雄--天生自然 PHP开发学习:表单和用户输入
<html> <head> <meta charset="utf-8"> <title>菜鸟教程(runoob.com)</t ...
随机推荐
- .net Form 的Autoscalemode属性
.net Form 的Autoscalemode属性如果设置成Font 将会随着系统字体的大小来改变form大小 有时候会造成布局混乱,小心使用
- 2020/2/17 zzcms8.2 PHP代码审计
0x00 看网站结构 ********************************* * * * ZZCMS产品版目录结构 * * * ****************************** ...
- P 1034 有理数四则运算
转跳点:
- AD走圆弧走线
美式键盘: “shift + 空格”
- Egret - EUI - 隐藏滚动条
<e:Skin> <e:VScrollBar autoVisibility="false" visible="false"/> < ...
- mybatis初步配置容易出现的问题
The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone. You ...
- 使用eclipse创建maven时遇到的问题
转自https://www.cnblogs.com/hongmoshui/p/7994759.html 1.在eclipse中用maven创建项目,右键new>>Maven Proje ...
- 吴裕雄--天生自然 JAVASCRIPT开发学习: Break 和 Continue 语句
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- PAT Advanced 1151 LCA in a Binary Tree (30) [树的遍历,LCA算法]
题目 The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both ...
- c 转二进制
int nData = 1568;//转二进制 for (int i = sizeof(int) * 8 - 1; i >= 0; i--){ if ((nData >>i) &am ...