📜  PHP表单处理

📅  最后修改于: 2021-11-08 02:16:40             🧑  作者: Mango

在本文中,我们将讨论如何在PHP处理表单。 HTML 表单用于将用户信息发送到服务器并将结果返回给浏览器。例如,如果您想获取您网站访问者的详细信息,并向他们发送好的想法,您可以通过表单处理的方式收集用户信息。然后,可以在客户端或服务器端验证信息。最终结果通过相应的 Web 浏览器发送到客户端。要创建 HTML 表单,应使用form标签。

表单标签的属性:

Attribute Description
name or id It specifies the name of the form and is used to identify individual forms.
action It specifies the location to which the form data has to be sent when the form is submitted.
method It specifies the HTTP method that is to be used when the form is submitted. The possible values are get and post. If get method is used, the form data are visible to the users in the url. Default HTTP method is get.
encType It specifies the encryption type for the form data when the form is submitted.
novalidate It implies the server not to verify the form data when the form is submitted.

表单中使用的控件:表单处理包含一组控件,客户端和服务器可以通过这些控件进行通信和共享信息。表单中使用的控件是:

  • 文本框:文本框允许用户提供单行输入,可用于获取名称、搜索菜单等值。
  • Textarea: Textarea 允许用户提供多行输入,可用于获取地址、消息等值。
  • 下拉:下拉或组合框允许用户从值列表中选择一个值。
  • 单选按钮:单选按钮允许用户从给定的一组选项中只选择一个选项。
  • 复选框:复选框允许用户从给定的选项集中选择多个选项。
  • 按钮:按钮是可用于提交表单的可点击控件。

创建一个简单的 HTML 表单:上面给出的所有表单控件都是根据标签的type属性使用input标签设计的。在下面的脚本中,提交表单时,没有完成任何事件处理机制。事件处理是指提交表单时完成的过程。这些事件处理机制可以通过使用 javaScript 或PHP来完成。但是,JavaScript 仅提供客户端验证。因此,我们可以使用PHP进行表单处理。

HTML代码:



  

    Simple Form Processing

  

    
        FirstName:                  
        
          LastName                  
        
          Address                  
        
          Email Address:                  
        
          Password:                  
        
        


  

    Simple Form Processing

  

    

Form Processing using PHP

    
                     "                          . $error . "

";                     }                 }                 ?>                    FirstName:                                    *                 
                
                Last Name:                                    *                 
                
                  Address:                                    *                 
                
                  Email:                                    *                 
                
                  Password:                                     *                 
                
                  Gender:                  Male                 Female                 
                
                              
    INPUT RECEIVED
";                    echo "";                    echo "";                    echo "";                    echo "";                    echo "";                    echo "";                    echo "";                    echo "";                    echo "";                    echo "";                    echo "";                    echo "";                    echo "";                    echo "";                    echo "";                    echo "";                    echo "";                    echo "";                    echo "";                    echo "";                    echo "";                    echo "";                    echo "";                    echo "";                    echo "";                    echo "";                    echo "";                    echo "";                    echo "";                    echo "
ParameterValue
First Name".$firstname."
Last Name".$lastname."
Address".$address."
Email Address" .$emailaddress."
Password".$password."
Gender".$gender."
";         }       }     ?>   
  • 输出:
  • 注意:当PHP和 HTML 编码在单个文件中时,该文件应保存为PHP.在表单中,action 参数的值应该是文件名。