search
HomeWeb Front-endHTML TutorialDetailed explanation of the working process of form form

1. Form

1. The role of the form

HTML forms are used to receive different types of user input. When the user submits the form, the data is transmitted to the server, thereby realizing the interaction between the user and the server. Web server interaction.

 2. Working mechanism of the form


  

 3. Form definition (

Tag)

An HTML form is an area containing form elements. Forms are created using the

tag. Forms can contain 1      2      3     

 3. Form Attribute

Action: Specifies where to send form data when the form is submitted. The value of action is: first, a URL (absolute URL/relative URL), generally pointing to a program on the server side. The program receives the data submitted by the form (that is, the form element value) and processes it accordingly. For example,

, when the user submits this form, the server will execute the URL. A general handler named "reg.ashx" on www.cnblogs.com/". Second, use the URL address of the mailto protocol, which will send the form content as an email. This situation is relatively rare , because it requires that the mail-sending program is installed and correctly set up on the visitor's computer. Third, a null value, if the action is empty or not written, means submission to the current page. .

  method:该属性定义浏览器将表单中的数据提交给服务器处理程序的方式。关于method的取值,最常用的是get和post。第一,使用get方式提交表单数据,Web浏览器会将各表单字段元素及其数据按照URL参数格式附在

标签的action属性所指定的URL地址后面发送给Web服务器;由于URL的长度限制,使用get方式传送的数据量一般限制在1KB以下。第二,使用post方式,浏览器会将表单数据作为HTTP请求体的一部分发送给服务器。一般来说,使用post方式传送的数据量要比get方式传递的数据量大;根据HTML标准,如果处理表单的服务器程序不会改变服务器上存储的数据,则应采用get方式(比如查询),如果表单处理的结果会引起服务器上存储的数据的变化,则应该采用post方式(比如增删改操作)。第三,其它方式(Head、PUT、DELETE、TRACE 或 OPTIONS等)。其实,最初HTTP标准对各种操作都规定了相应的method,但后来很多都没有被遵守,大部分情况只是使用get或post就OK。关于更多的各种method方式的区别,由于我目前对HTTP协议了解的不多,不敢妄下结论。很多园友的讨论也好像不是很深入,大家争论比较多。

  target:该属性规定在何处显示action属性中指定的URL所返回的结果。取值有_blank(在新窗口中打开)、_self(在相同的框架中打开,默认值)、_parent(在父框架中打开)、_top(在整个窗口中打开)和framename(在指定的框架中打开)。

  title:设置网站访问者的鼠标放在表单上的任意位置停留时,浏览器用小浮标显示的文本。

    enctype:规定在发送到服务器之前应该如何对表单数据进行编码。取值:默认值为 "application/x-www-form-urlencoded",在发送到服务器之前,所有字符都会进行编码(空格转换为 "+" 加号,特殊符号转换为 ASCII HEX 值);“multipart/form-data”:不对字符编码。在使用包含文件上传控件的表单时,必须使用该值。

  name:表单的名称。注意和id属性的区别:name属性是和服务器通信时使用的名称;而id属性是浏览器端使用的名称,该属性主要是为了方便客户端编程,而在css和javascript中使用的。

二、表单元素

  1.单行文本框(input 的type 属性的默认值就是"text")

<input>

  以下是单行文本框的主要属性:

    size:指定文本框的宽度,以字符个数为单位;在大多数浏览器中,文本框的缺省宽度是20个字符。

    value:指定文本框的默认值,是在浏览器第一次显示表单或者用户单击按钮之后在文本框中显示的值。

    maxlength:指定用户输入的最大字符长度。

    readonly:只读属性,当设置readonly属性后,文本框可以获得焦点,但用户不能改变文本框中的value。

    disabled:禁用,当文本框被禁用时,不能获得焦点,当然,用户也不能改变文本框的值。并且在提交表单时,浏览器不会将该文本框的值发送给服务器。

  2.密码框 

<input>

  3.单选按钮

  使用方式:使用name相同的一组单选按钮,不同radio设定不同的value值,这样通过取指定name的值就可以知道谁被选中了,不用单独的判断。单选按钮的元素值由value属性显式设置,表单提交时,选中项的value和name被打包发送,不显式设置value。

    <input> 
    <input>

  4.复选框

  使用复选按钮组,即name相同的一组复选按钮,复选按钮表单元素的元素值由value属性显式设置,表达提交时,所有选中项的value和name被打包发送

不显式设置value。复选框的checked属性表示是否被选中,或者(推荐)checked、readonly等这种一个可选值的属性都可以省略属性值。

     <input> 
     <input>
     <input>

  5.隐藏域

  隐藏域通常用于向服务器提交不需要显示给用户的信息。

<input>

  6.文件上传

  使用file,则form的enctype必须设置为multipart/form-data,method属性为POST。

<input>

  7.下拉框

  与嵌套的

  将一个option设置为选中:或者(推荐方式)就可以将这个项设定为选择项。如何实现“不选择”,添加一个

  select分组选项,可以使用optgroup对数据进行分组,分组本身不会被选择,无论对于下拉列表还是列表框都适用。  

  

 1     <select>
 2         <optgroup>
 3             <option>Gambia</option>
 4             <option>Madagascar</option>
 5             <option>Namibia</option>
 6         </optgroup>
 7         <optgroup>
 8             <option>France</option>
 9             <option>Russia</option>
10             <option>UK</option>
11         </optgroup>
12         <optgroup>
13             <option>Canada</option>
14             <option>Mexico</option>
15             <option>USA</option>
16         </optgroup>
17     </select>

  8.多行文本

  多行文本,cols=“50”、rows=“15”属性表示行数和列数,不指定则浏览器采取默认显示。

1    <textarea>
2  多行文本框的初始显示内容 
3    </textarea>

  9.标签

   在前可以写普通的文本来修饰,但是单击修饰文本的时候input并不会得到焦点,而用label则可以,for属性指定要修饰的控件的id,;”,然后按下alt+u(了解)。accesskey=“u“,label的另一个属性。注意:要为被修饰的控件设置一个唯一的id。我觉得标签对这两个标签是非常有用的。

1   <input>

   10.

标签

  fieldset标签将控件划分一个区域,看起来更规整。

2    爱好 3     <input> 4     <input> 5     <input> 6 

  11.提交按钮

  当用户单击的提交按钮时,表单数据会提交给

标签的action属性所指定的服务器处理程序。中文IE下默认按钮文本为“提交查询”,可以设置value属性修改按钮的显示文本。 
    <input>

  12.重置按钮

  当用户单击按钮时,表单中的值被重置为初始值。在用户提交表单时,重置按钮的name和value不会提交给服务器。

<input>

  13.普通按钮

  普通按钮通常用于单击执行一段脚本代码。

    <input>

  14.图像按钮

  图像按钮的src属性指定图像源文件,它没有value属性。图像按钮可代替,而现在也可以通过css直接将按钮的外观设置为一幅图片

  <input>

三、表单示例

  该示例是使用表单实现的一个简单的注册页面,使用表格布局。

  1 nbsp;HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2 
  3 
  4     <title>注册页面</title>
  5     <style>
  6         table
  7         {
  8             width: 450px;
  9             border: 1px solid red;
 10             background-color: #FFCB29;
 11             border-collapse: collapse;
 12         }
 13         td
 14         {
 15             width: 200;
 16             height: 40px;
 17             border: 1px solid black;
 18         }
 19         span
 20         {
 21             background-color: red;
 22         }
 23     </style>
 24 
 25 
 26     
 27       28           29               32               35           36           37               40               43           44           45               48               51           52           53               56               70           71           72               75               80           81           82               85               90           91           92               95              104          105          106              109              112          113          114              117              121          122     
 30  用户名:  31               33                 <input>  34             
 38  密码:  39               41                 <input>  42             
 46  确认密码:  47               49                 <input>  50             
 54  请选择市:  55               57                   69             
 73  请选择性别:  74               76                 <input>
 83  请选择职业:  84               86                 <input>  87                 <input>  88                 <input>  89             
 93  请选择爱好:  94               96                 
 97                     你的爱好  98                     <input>  99                     <input> 100                     <input> 101                     <input> 102                 
103             
107  备注: 108              110                  111             
115                   116              118                 <input> 119                 <input> 120             
123     
124  125 

【相关推荐】

1. HTML免费视频教程

2. 详解form标签中的method属性

3. 带你掌握HTML中table和form表单

4. 详解html中form表单的参数和属性

5. 带你掌握HTML中table和form表单

The above is the detailed content of Detailed explanation of the working process of form form. For more information, please follow other related articles on the PHP Chinese website!

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
HTML in Action: Examples of Website StructureHTML in Action: Examples of Website StructureMay 05, 2025 am 12:03 AM

HTML is used to build websites with clear structure. 1) Use tags such as, and define the website structure. 2) Examples show the structure of blogs and e-commerce websites. 3) Avoid common mistakes such as incorrect label nesting. 4) Optimize performance by reducing HTTP requests and using semantic tags.

How do you insert an image into an HTML page?How do you insert an image into an HTML page?May 04, 2025 am 12:02 AM

ToinsertanimageintoanHTMLpage,usethetagwithsrcandaltattributes.1)UsealttextforaccessibilityandSEO.2)Implementsrcsetforresponsiveimages.3)Applylazyloadingwithloading="lazy"tooptimizeperformance.4)OptimizeimagesusingtoolslikeImageOptimtoreduc

HTML's Purpose: Enabling Web Browsers to Display ContentHTML's Purpose: Enabling Web Browsers to Display ContentMay 03, 2025 am 12:03 AM

The core purpose of HTML is to enable the browser to understand and display web content. 1. HTML defines the web page structure and content through tags, such as, to, etc. 2. HTML5 enhances multimedia support and introduces and tags. 3.HTML provides form elements to support user interaction. 4. Optimizing HTML code can improve web page performance, such as reducing HTTP requests and compressing HTML.

Why are HTML tags important for web development?Why are HTML tags important for web development?May 02, 2025 am 12:03 AM

HTMLtagsareessentialforwebdevelopmentastheystructureandenhancewebpages.1)Theydefinelayout,semantics,andinteractivity.2)SemantictagsimproveaccessibilityandSEO.3)Properuseoftagscanoptimizeperformanceandensurecross-browsercompatibility.

Explain the importance of using consistent coding style for HTML tags and attributes.Explain the importance of using consistent coding style for HTML tags and attributes.May 01, 2025 am 12:01 AM

A consistent HTML encoding style is important because it improves the readability, maintainability and efficiency of the code. 1) Use lowercase tags and attributes, 2) Keep consistent indentation, 3) Select and stick to single or double quotes, 4) Avoid mixing different styles in projects, 5) Use automation tools such as Prettier or ESLint to ensure consistency in styles.

How to implement multi-project carousel in Bootstrap 4?How to implement multi-project carousel in Bootstrap 4?Apr 30, 2025 pm 03:24 PM

Solution to implement multi-project carousel in Bootstrap4 Implementing multi-project carousel in Bootstrap4 is not an easy task. Although Bootstrap...

How does deepseek official website achieve the effect of penetrating mouse scroll event?How does deepseek official website achieve the effect of penetrating mouse scroll event?Apr 30, 2025 pm 03:21 PM

How to achieve the effect of mouse scrolling event penetration? When we browse the web, we often encounter some special interaction designs. For example, on deepseek official website, �...

How to modify the playback control style of HTML videoHow to modify the playback control style of HTML videoApr 30, 2025 pm 03:18 PM

The default playback control style of HTML video cannot be modified directly through CSS. 1. Create custom controls using JavaScript. 2. Beautify these controls through CSS. 3. Consider compatibility, user experience and performance, using libraries such as Video.js or Plyr can simplify the process.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),