Php 简明教程

PHP - Form Handling

HTML Forms 在 PHP Web 应用程序中扮演着重要的角色。虽然纯粹由 HTML 编写的网页是一个静态网页,但 HTML 表单组件是一个重要的特性,有助于实现交互性和呈现动态内容。PHP 的表单处理功能可以在处理之前验证从用户收集的数据。

HTML Forms play an important role in PHP web applications. Although a webpage composed purely with HTML is a static webpage, the HTML form component is an important feature that helps in bringing interactivity and rendering dynamic content. PHP’s form handling functionality can validate data collected from the user, before processing.

HTML 表单是各种表单控件的集合,例如文本字段、复选框、单选按钮等,用户可以使用这些表单控件进行交互、输入或选择某些数据,这些数据可以由 JavaScript(客户端处理)在本地处理,也可以借助 PHP 等服务器端编程脚本发送到远程服务器进行处理。

An HTML Form is a collection various form controls such as text fields, checkboxes, radio buttons, etc., with which the user can interact, enter or choose certain data that may be either locally processed by JavaScript (client-side processing), or sent to a remote server for processing with the help of server-side programming scripts such as PHP.

一个或多个表单控件元素被放在 <form> 和 </form> 标记内。 form element 的特点是具有不同的属性,例如名称、操作和方法。

One or more form control elements are put inside <form> and </form> tags. The form element is characterized by different attributes such as name, action, and method.

<form [attributes]>
   Form controls
</form>

Form Attributes

在 HTML 表单元素的众多属性中,经常需要和定义以下属性 −

Out of the many attributes of the HTML form element, the following attributes are often required and defined −

Action Attribute

表示处理表单提交的 URL 的字符串。例如, http://example.com/test.php 。若要将表单数据提交至定义 HTML 表单的相同 PHP 脚本,请使用 PHP_SELF 服务器变量 −

a string representing the URL that processes the form submission. For example, http://example.com/test.php. To submit the for-data to the same PHP script in which the HTML form is defined, use the PHP_SELF server variable −

<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">

Enctype Attribute

指定在将表单数据发送到服务器之前对其进行编码的方法。可能的值为 −

specifies the method using which the form-data should be encoded before sending it to the server. Possible values are −

  1. application/x-www-form-urlencoded − The default value.

  2. multipart/form-data − Use this if the form contains <input> elements with type=file.

  3. text/plain − Useful for debugging purposes.

Method Attribute

表示提交表单所用 HTTP 方法的字符串。以下方法是 method 属性的可能值 −

a string representing the HTTP method to submit the form with. The following methods are the possible values of method attribute −

  1. post − The POST method; form data sent as the request body.

  2. get (default) − The GET; form data appended to the action URL with a "?" separator. Use this method when the form has no side effects.

  3. dialog − When the form is inside a <dialog>, closes the dialog and causes a submit event to be fired on submission, without submitting data or clearing the form.

Name Attribute

表单的名称。该值不能是空字符串,且如果同一 HTML 文档中存在多个表单,则它必须是唯一的。

The name of the form. The value must not be the empty string, and must be unique if there are multiple forms in the same HTML document.

Target Attribute

一个指示提交表单后显示响应的位置的字符串。应为下列选项之一:

a string that indicates where to display the response after submitting the form. Should be one of the following −

  1. _self (default) − Load into the same browsing context as the current one.

  2. _blank − Load into a new unnamed browsing context.

  3. _parent − Load into the parent browsing context of the current one.

  4. _top − Load into the top-level browsing context (an ancestor of the current one and has no parent).

因此,在一个 PHP Web 应用程序中使用的典型 HTML 表单如下所示:

Hence, a typical HTML form, used in a PHP web application looks like −

<form name="form1" action="<?php echo $_SERVER['PHP_SELF'];?>" action="POST">
   Form controls
</form>

Form Elements

HTML 表单设计为不同类型的控件或元素。用户可以与这些控件交互,以输入数据或从呈现的可用选项中进行选择。下面描述了一些元素:

A HTML form is designed with different types of controls or elements. The user can interact with these controls to enter data or choose from the available options presented. Some of the elements are described below −

Input Element

input 元素表示一个数据域,允许用户输入和/或编辑数据。

The input element represents a data field, which enables the user to enter and/or edit the data.

input 元素的 type 属性控制数据。input 元素可以是以下类型:

The type attribute of INPUT element controls the data. The INPUT element may be of the following types −

用于输入单行文本的文本域。

A text field to enter a single line text.

<input type="text" name="employee">

一个屏蔽输入字符的单行文本域。

A single line text filed that masks the entered characters.

<input type="password" name="pwd"><br>

一个可复选的矩形框,是从预定义列表中获取的一个或多个值。

A rectangular checkable box which is a set of zero or more values from a predefined list.

<input type="checkbox" id="s1" name="sport1" value="Cricket">
<label for="s1">I like Cricket</label><br>
<input type="checkbox" id="s2" name="sport2" value="Football">
<label for="s2">I like Football</label><br>
<input type="checkbox" id="s3" name="sport3" value="Tennis">
<label for="s3">I like Tennis</label><br><br>

此类型呈现一个带有两种状态(开或关)的可圆形单击按钮,通常是单选按钮组中的一个按钮。

This type renders a round clickable button with two states (ON or OFF), usually a part of multiple buttons in a radio group.

<input type="radio" id="g1" name="gender" value="Male">
<label for="g1">Male</label><br>
<input type="radio" id="g2" name="female" value="Female">
<label for="g2">Female</label><br>

input 类型呈现一个标有标题文件的按钮,允许用户从客户端文件系统选择一个文件,通常要上载到服务器。表单的 enctype 属性必须设置为“multipart/form-data”。

The input type renders a button captioned file and allows the user to select a file from the client filesystem, usually to be uploaded on the server. The form’s enctype attribute must be set to "multipart/form-data"

<input type="file" name="file">

一个单行文本域,经过自定义以接受一个符合有效电子邮件 ID 的字符串。

A single line text field, customized to accept a string conforming to valid email ID.

一个单行文本域,经过自定义以接受一个符合有效 URL 的字符串。

A single line text filed customized to accept a string conforming to valid URL.

此 input 元素呈现一个按钮,单击此按钮时,它会启动将表单数据提交到当前表单的 action 属性中指定的 URL。

This input element renders a button, which when clicked, initiates the the submission of form data to the URL specified in the action attribute of the current form.

<input type="submit" name="Submit">

Select Element

Select 元素表示用于在一组选项中进行选择的控件。每个选项都由 Select Control 的 option 属性定义。例如 −

The select element represents a control for selecting amongst a set of options. Each choice is defined with option attribute of Select Control. For example −

<select name="Subjects" id="subject">
   <option value="Physics">Physics</option>
   <option value="Chemistry">Chemistry</option>
   <option value="Maths">Maths</option>
   <option value="English">English</option>
</select>

Form Example

让我们使用这些表单元素来设计一个 HTML 表单并将其发送到 PHP_SELF 脚本

Let us use these form elements to design a HTML form and send it to a PHP_SELF script

<html>
<body>
   <form method = "post" action = "<?php
      echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
      <table>
         <tr>
            <td>Name:</td>
            <td><input type = "text" name = "name"></td>
         </tr>
         <tr>
            <td>E-mail: </td>
            <td><input type = "email" name = "email"></td>
         </tr>
         <tr>
            <td>Website:</td>
            <td><input type = "url" name = "website"></td>
         </tr>
         <tr>
            <td>Classes:</td>
            <td><textarea name = "comment" rows = "5" cols = "40"></textarea></td>
         </tr>
         <tr>
            <td>Gender:</td>
            <td>
               <input type = "radio" name = "gender" value = "female">Female
               <input type = "radio" name = "gender" value = "male">Male
            </td>
         </tr>
         <td>
            <input type = "submit" name = "submit" value = "Submit">
         </td>
      </table>
   </form>
   <?php
      $name = $email = $gender = $comment = $site = "";

      if ($_SERVER["REQUEST_METHOD"] == "POST") {
         $name = $_POST["name"];
         $email = $_POST["email"];
         $name = $_POST["name"];
         $comment = $_POST["comment"];
         $gender = $_POST["gender"];
         $site = $_POST["website"];
      }
      echo "<h2>Your given values are as:</h2>";
      echo $name;
      echo "<br>";

      echo $email;
      echo "<br>";

      echo $site;
      echo "<br>";

      echo $comment;
      echo "<br>";

      echo $gender;
   ?>
</body>
</html>

它将生成以下 output

It will produce the following output

php form handling