PHP 表单


2021年12月17日, Learn eTutorial
2331

在本 PHP 教程中,您将学习有关表单以及 PHP 中各种表单验证的所有知识。我们将详细讨论创建表单和验证这些表单。

PHP 中的表单是什么?

众所周知,PHP 是一种用于创建动态网页的服务器端脚本语言。表单用于接受用户的数据,这些收集的数据将存储在数据库中,或者这些数据将被处理并向用户提供输出。 表单是一种 HTML 标签,其中包含图形用户界面元素,如输入框、复选框、单选按钮等。表单由 <form>...</form> 标签定义,而 GUI 组件(如输入)由表单元素定义。PHP 的 GET 和 POST 方法用于将数据从用户传输到服务器。该方法将在 HTML 表单标签中定义。 

PHP : Html Form

使用 GET 方法创建表单

GET 方法用于在 HTML 表单提交后收集数据。当表单使用 get 方法发送数据时,数据会出现在查询字符串中,因此值不会被隐藏。超全局数组变量 $_GET 包含通过 URL 传来的值。由于 get 方法通过 URL 发送数据,因此其字符数限制为 2000 个。get 方法仅用于将非敏感数据发送到服务器,因为它在 URL 中可见,所以传输密码等敏感数据不安全。

示例:registration.html


<!DOCTYPE html>
<html lang="en">
<head>
  <title>Registration</title>
  <style>
   .row {
    display: flex;
    justify-content: space-between;
    margin-bottom: 5px;
   }
   .txtBox {
    width: 200px;
   }
   .btns {
    display: flex;
    justify-content: center;
    margin-top: 10px;
   }
  </style>
 </head>
 <body>
  <div class="content" >
   <h2 >Registration Form</h2>
   <form acti method="get">
    <div class="row">
     <label for="fName">Fisrt Name: </label>
     <input type="text" name="fName" class="txtBox" id="fName" />
    </div>
    <div class="row">
     <label for="lName">Last Name: </label>
     <input type="text" name="lName" class="txtBox" id="lName" />
    </div>
    <div class="row">
     <label for="phone">Phone: </label>
     <input type="text" name="phone" class="txtBox" id="phone" />
    </div>
    <div class="row">
     <label for="dob">Date of Birth: </label>
     <input type="date" name="dob" class="txtBox" id="dob" />
    </div>
    <div class="row">
     <label for="adrs">Address: </label>
     <textarea
      name="adrs"
      id="adrs"
      class="txtBox"
      cols="30"
      rows="10"
     ></textarea>
    </div>
    <div class="btns">
     <input type="submit" class="btn" value="Next" name="submit" />
     <input type="reset" class="btn" value="Clear" />
    </div>
   </form>
  </div>
 </body>
</html>

输出


PHP - Form

示例:registration_view.php


<!DOCTYPE html>
<html lang="en">
<head>     
<meta charset="UTF-8">  
<meta http-equiv="X-UA-Compatible"> 
<meta name="viewport">
    <title>Registration view</title>
</head>
<?php
if (isset($_GET['submit'])) {
    $fName = $_GET['fName']; 
    $lName = $_GET['lName'];  
    $dob = $_GET['dob'];     
    $ph= $_GET['ph'];
    $adrs = $_GET['adrs'];
    $fullName = $fName . " " . $lName;
}
?>
<body>
    <h2>The Entered Details are:</h2>
    <p>
        <strong>Full Name: </strong> <?php echo $fullName; ?><br>
        <strong>Phone Number: </strong><?php echo $ph; ?><br>
        <strong>Date of Birth: </strong><?php echo $dob; ?><br>
        <strong>Address: </strong><?php echo $adrs; ?>
    </p>
</body>
</html>

输出


PHP - Form

使用POST方法创建表单

POST 方法用于在 HTML 表单提交后收集数据。由于表单使用 method post 传输数据时,数据在查询字符串中不可用,因此通过这种方式维护了安全标准。Post 请求通常用于提交大量数据的表单,例如文件上传、图片上传、登录表单、注册表单等。由于通过 post 请求提供的数据对 URL 浏览器不可见,因此是安全的。通过 post 请求,您可以提交大量数据。

示例:registration.html


<!DOCTYPE html>
<html lang="en">
 <head>
  <meta charset="UTF-8" />   <meta http-equiv="X-UA-Compatible" c />   <meta name="viewport" c />
  <title>Registration</title>
  <style>
   .row {
    display: flex;
    justify-content: space-between;
    margin-bottom: 5px;
   }
   .txtBox {
    width: 200px;
   }
   .btns {
    display: flex;
    justify-content: center;
    margin-top: 10px;
   }
  </style>
 </head>
 <body>   <div class="content" >    <h2 >Registration Form</h2>      
<form action =" registration_view.php " method="post">
    <div class="row">
     <label for="fName">Fisrt Name: </label>
     <input type="text" name="fName" class="txtBox" id="fName" />
    </div>
    <div class="row">
     <label for="lName">Last Name: </label>
     <input type="text" name="lName" class="txtBox" id="lName" />
    </div>
    <div class="row">
     <label for="phone">Phone: </label>
     <input type="text" name="phone" class="txtBox" id="phone" />
    </div>
    <div class="row">
     <label for="dob">Date of Birth: </label>
     <input type="date" name="dob" class="txtBox" id="dob" />
    </div>
    <div class="row">
     <label for="adrs">Address: </label>
     <textarea
      name="adrs" class="txtBox"
      cols="30"
      rows="10"
     ></textarea>
    </div>
    <div class="btns">
     <input type="submit" class="btn" value="Next" name="submit" />
     <input type="reset" class="btn" value="Clear" />
    </div>
   </form>
  </div>
 </body>
</html>



输出


PHP - Form

示例:registration_view.php


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" >
    <meta name="viewport" >
    <title>Registration view</title>
</head>
<?php
if (isset($_POST['submit'])) {
    $fName = $_POST['fName'];
    $lName = $_POST['lName'];
    $ph = $_POST['ph'];
    $dob = $_POST['dob'];
    $adrs = $_POST['adrs'];
    $fullName = $fName . " " . $lName;
}
?>
<body>
    <h2>The Entered Details are:</h2>
    <p>
        <strong>Full Name: </strong> <?php echo $fullName; ?><br>
        <strong>Phone Number: </strong><?php echo $ph; ?><br>
        <strong>Date of Birth: </strong><?php echo $dob; ?><br>
        <strong>Address: </strong><?php echo $adrs; ?>
    </p>
</body>
</html>

输出


PHP - Form

PHP中GET和POST方法的区别

GET POST

Get 请求的数据量有限,因为数据被封装在头部中。

在 post 请求中,可以发送大量数据,因为数据包含在正文中。

Get 请求不安全,因为数据在 URL 中可见。

Post 请求是安全的,因为数据在 URL 中不可见。

Get 请求可以被收藏。

Post 请求不能被收藏。

Get 请求是幂等的。这意味着第二次请求将被忽略,直到收到第一次请求的响应。

Post 请求不是幂等的

 

PHP 中的表单验证是什么?

验证是指验证用户输入的过程。在 PHP 中,有两种验证形式。它们列在下面
客户端验证:验证在客户端计算机的 Web 浏览器上进行。
服务器端验证:数据上传后,会传输到服务器,服务器会在服务器计算机上执行验证测试。

应用于字段的各种验证规则

字段 验证规则

姓名

只能包含字母和空格

邮箱

应包含 '@' 和 '.'

网站

应只包含一个有效的 URL

单选按钮

应至少选择一项

复选框

应至少选择一项

服务器端验证

如何检查 URL 是否有效?

要检查输入的 URL 是否有效,我们会检查属于有效关键字(如 https、ftp、www、a-z、0-9 等)的字符。


$website = input($_POST["site"]);
if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website)) {
   $websiteErr = "Invalid URL"; 
}

如何检查电子邮件是否有效?

要检查输入的电子邮件是否有效,我们会检查字符并检查电子邮件中是否包含 '@' 和 '.'。


$email = input($_POST["email"]);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
   $emailErr = "Invalid format and please re-enter valid email"; 
}

输出


PHP - Form

检查表单中的字段是否有效?

示例:registration.php


<!DOCTYPE html>
<html lang="en">
<head>
 <title>Registration</title>
 <style>
  .row {
   display: flex;
   justify-content: space-between;
   margin-bottom: 5px;
  }
  .txtBox {
   width: 200px;
  }

  .btns {
   display: flex;
   justify-content: center;
   margin-top: 10px;
  }
  .error {
   color: red
  }
 </style>
</head>
<div class="row">
    <label for="lName">Last Name: </label>
    <span class="error"> <?php echo $lNameErr; ?></span>
    <input type="text" name="lName" class="txtBox" id="lName" />

   </div>
   <div class="row">
    <label for="email">Email: </label>
    <span class="error"> <?php echo $emailErr; ?></span>
    <input type="text" name="email" class="txtBox" id="email" />

   </div>
   <div class="row">
    <label for="phone">Phone: </label>
    <input type="text" name="phone" class="txtBox" id="phone" />
   </div>
   <div class="row">
    <label for="adrs">Address: </label>
    <textarea name="adrs" id="adrs" class="txtBox" cols="30" rows="10"></textarea>
   </div>
   <div class="btns">
    <input type="submit" class="btn" value="Next" name="submit" />
    <input type="reset" class="btn" value="Clear" />
   </div>
  </form>
 </div>
 <?php

 if ($_SERVER["REQUEST_METHOD"] == "POST") {
  echo "<h2>Your given values are as:</h2>";
  echo "<p>Full Name: $fName $lName <br> Email: $email <br> Phone: $phone <br> Address: $adrs </p>";
 }

 function test_input($data)
 {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
 }

 ?>

</body>
</html>

输出


PHP - Form

PHP - Form