PHP基础篇-表单完整实例
来源:互联网 发布:淘宝客cms系统 编辑:程序博客网 时间:2024/05/16 12:40
学习资料来源于:菜鸟教程
<!DOCTYPE HTML><html><head><style>.error {color: #FF0000;}</style></head><body><?php// define variables and set to empty values$nameErr = $emailErr = $genderErr = $websiteErr = "";$name = $email = $gender = $comment = $website = "";if ($_SERVER["REQUEST_METHOD"] == "POST"){ if (empty($_POST["name"])) {$nameErr = "Name is required";} else { $name = test_input($_POST["name"]); // check if name only contains letters and whitespace if (!preg_match("/^[a-zA-Z ]*$/",$name)) { $nameErr = "Only letters and white space allowed"; } } if (empty($_POST["email"])) {$emailErr = "Email is required";} else { $email = test_input($_POST["email"]); // check if e-mail address syntax is valid if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email)) { $emailErr = "Invalid email format"; } } if (empty($_POST["website"])) {$website = "";} else { $website = test_input($_POST["website"]); // check if URL address syntax is valid (this regular expression also allows dashes in the URL) if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website)) { $websiteErr = "Invalid URL"; } } if (empty($_POST["comment"])) {$comment = "";} else {$comment = test_input($_POST["comment"]);} if (empty($_POST["gender"])) {$genderErr = "Gender is required";} else {$gender = test_input($_POST["gender"]);}}function test_input($data){ $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); return $data;}?><h2>PHP Form Validation Example</h2><p><span class="error">* required field.</span></p><form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> Name: <input type="text" name="name" value="<?php echo $name;?>"> <span class="error">* <?php echo $nameErr;?></span> <br><br> E-mail: <input type="text" name="email" value="<?php echo $email;?>"> <span class="error">* <?php echo $emailErr;?></span> <br><br> Website: <input type="text" name="website" value="<?php echo $website;?>"> <span class="error"><?php echo $websiteErr;?></span> <br><br> Comment: <textarea name="comment" rows="5" cols="40"><?php echo $comment;?></textarea> <br><br> Gender: <input type="radio" name="gender" <?php if (isset($gender) && $gender=="female") echo "checked";?> value="female">Female <input type="radio" name="gender" <?php if (isset($gender) && $gender=="male") echo "checked";?> value="male">Male <span class="error">* <?php echo $genderErr;?></span> <br><br> <input type="submit" name="submit" value="Submit"></form><?phpecho "<h2>Your Input:</h2>";echo $name;echo "<br>";echo $email;echo "<br>";echo $website;echo "<br>";echo $comment;echo "<br>";echo $gender;?></body></html>
0 0
- PHP基础篇-表单完整实例
- php完整表单验证实例
- PHP表单使用实例
- 21. PHP 表单验证 - 完成表单实例
- PHP经典实例读书笔记--表单
- 实例学习 PHP 之表单处理篇(一)
- 【php基础班】第3天 超链接a、table标签、表单、透明的flash实例
- 实例学习PHP之程序基础篇
- php基础实例
- PHP基础语法实例
- php中单件模式(singleton pattern)完整实例
- php封装的mysqli类完整实例
- php xdebug 完整篇
- php基础算法之完整“菱形”
- php表单发送到myadmin实例
- PHP表单实例(简单例子)
- HTML基础笔记之表单实例
- PHP 学习笔记(二):表单:表单处理,表单验证,表单实例
- 【FPGA黑金开发板】Verilog HDL那些事儿--串口模块(十一)
- NYOJ 518 取球游戏(博弈问题)
- MySql数据库,对varchar类型字段str进行where str=0条件查询时,查询结果是什么
- 33. BOM
- Eclipse JAVA文件注释乱码
- PHP基础篇-表单完整实例
- linux(知识)
- 【UIKit-124-7】#import <UIKit/UIView.h>
- servlet+JDBC实现登录 增删查改 分页显示
- MySQL存储引擎
- 动态链接库中与地址无关代码(PIC)对于地址引用的处理
- 生成特定分布随机数的方法
- 如何看懂C语言程序
- iOS_AFNetworking_UIImageView+AFNetworking(实现图片异步加载)