Home  >  Article  >  php教程  >  php编程每天必学之表单验证,php编程表单

php编程每天必学之表单验证,php编程表单

WBOY
WBOYOriginal
2016-06-13 08:45:19874browse

php编程每天必学之表单验证,php编程表单

本文实例讲解了php表单验证的实现方法,分享给大家供大家参考,具体内容如下

1.PHP表单处理
welcome.html

<html>
<body>
  <form action="welcome.php" method="post">
    Name: <input type="text" name="name"><br>
    E-mail: <input type="text" name="email"><br>
    <input type="submit">
  </form>
</body>
</html>

welcome.php

<html>
<body>
<!--PHP 超全局变量 $_GET 和 $_POST 用于收集表单数据(form-data)-->

Welcome <&#63;php echo $_POST["name"]; &#63;><br>
Your email address is: <&#63;php echo $_POST["email"]; &#63;>

</body>
</html>

2.PHP表单验证完整实例

<html>
<head>
</head>
<body> 

<&#63;php
// 定义变量并设置为空值
$nameErr = $emailErr = $websiteErr = "";
$name = $email = $website = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
  if (empty($_POST["name"])) {
   $nameErr = "姓名是必填的";
  } else {
   $name = test_input($_POST["name"]);
   // 检查姓名是否包含字母和空白字符
   //preg_match() 函数检索字符串的模式,如果模式存在则返回 true,否则返回 false。
   if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
    $nameErr = "只允许字母和空格"; 
   }
  }
  
  if (empty($_POST["email"])) {
   $emailErr = "电邮是必填的";
  } else {
   $email = test_input($_POST["email"]);
   // 检查电子邮件地址语法是否有效
   if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email)) {
    $emailErr = "无效的 email 格式"; 
   }
  }
   
  if (empty($_POST["website"])) {
   $website = "";
  } else {
   $website = test_input($_POST["website"]);
   // 检查 URL 地址语法是否有效(正则表达式也允许 URL 中的斜杠)
   if (!preg_match("/\b(&#63;:(&#63;:https&#63;|ftp):\/\/|www\.)[-a-z0-9+&@#\/%&#63;=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website)) {
    $websiteErr = "无效的 URL"; 
   }
  }

}

function test_input($data) {
  //去除用户输入数据中不必要的字符(多余的空格、制表符、换行)
  $data = trim($data);
  //删除用户输入数据中的反斜杠(\)
  $data = stripslashes($data);
  //把特殊字符转换为 HTML 实体
  $data = htmlspecialchars($data);
  return $data;
}
&#63;>

<h2>PHP 验证实例</h2>
<p><span class="error">* 必需的字段</span></p>
<!--htmlspecialchars() 函数把特殊字符转换为 HTML 实体。-->
<form method="post" action="<&#63;php echo htmlspecialchars($_SERVER["PHP_SELF"]);&#63;>"> 
  <!--value:保留表单中的值-->
  姓名:<input type="text" name="name" value="<&#63;php echo $name;&#63;>">
  <span class="error">* <&#63;php echo $nameErr;&#63;></span>
  <br><br>
  邮箱:<input type="text" name="email" value="<&#63;php echo $email;&#63;>">
  <span class="error">* <&#63;php echo $emailErr;&#63;></span>
  <br><br>
  网址:<input type="text" name="website" value="<&#63;php echo $website;&#63;>">
  <span class="error"><&#63;php echo $websiteErr;&#63;></span>
  <br><br>
  <input type="submit" name="submit" value="提交"> 
</form>

<&#63;php
echo "<h2>您的输入:</h2>";
echo $name;
echo "<br>";
echo $email;
echo "<br>";
echo $website;
echo "<br>";
&#63;>

</body>
</html>

希望本文所述对大家学习php程序设计有所帮助。

您可能感兴趣的文章:

  • php下常用表单验证的正则表达式
  • php 表单验证实现代码
  • PHP开发中常用的三个表单验证函数使用小结
  • PHP表单验证的3个函数ISSET()、empty()、is_numeric()的使用方法
  • php tp验证表单与自动填充函数代码
  • 基于PHP+Ajax实现表单验证的详解
  • php用户注册页面利用js进行表单验证具体实例
  • ThinkPHP表单自动提交验证实例教程
  • ThinkPHP表单自动验证实例
  • thinkPHP实现表单自动验证
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn