Latest web development tutorials

HTML form

HTML Forms are used to collect different types of user input.


Examples

Online examples

Create a text field (Text field)
This example demonstrates how to create a text field in the HTML page. The user can write text in a text field.

Create a password field
This example demonstrates how to create an HTML password field.

(At bottom of this page you can find more examples.)


HTML form

Form is a region that contains the form elements.

Form element is to allow the user to enter content in the form, for example: text fields (textarea), drop-down lists, radio buttons (radio-buttons), checkboxes (checkboxes) and so on.

Form uses form tags <form> set:

<form>
.
input 元素
.
</form>


HTML Forms - Input elements

In most cases are used form tag is an input tag (<input>).

The input type is the type attribute (type) definitions. Most frequently used input types are as follows:


Text field (Text Fields)

Through text field <input type = "text"> tag to set when the user to type letters, numbers, and other content in a form, the text field will be used.

<form>
First name: <input type="text" name="firstname"><br>
Last name: <input type="text" name="lastname">
</form>

The browser displays the following:

First name:
Last name:

Note: The form itself is not visible.Also, in most browsers, the default width of the text field is 20 characters.


Password field

Password fields by tag <input type = "password"> to define:

<form>
Password: <input type="password" name="pwd">
</form>

The browser displays the following effects:

Password:

Note: The password field characters are not displayed in clear text, but with an asterisk or dots instead.


Radio button (Radio Buttons)

<Input type = "radio"> tag defines table alone box options

<form>
<input type="radio" name="sex" value="male">Male<br>
<input type="radio" name="sex" value="female">Female
</form>

The browser displays the following effects:

Male
Female

Check boxes (Checkboxes)

<Input type = "checkbox"> defines a checkbox. Users need to select one or several options from several given choices.

<form>
<input type="checkbox" name="vehicle" value="Bike">I have a bike<br>
<input type="checkbox" name="vehicle" value="Car">I have a car
</form>

The browser displays the following effects:

I have a bike
I have a car

Submit button (Submit Button)

<Input type = "submit"> defines the submit button.

When the user clicks the OK button, the contents of the form will be transferred to another file. Form action attribute defines the file name of the destination file. Defined by the action attribute of the file normally would have received input data related to the treatment. :

<form name="input" action="html_form_action.php" method="get">
Username: <input type="text" name="user">
<input type="submit" value="Submit">
</form>

The browser displays the following effects:

Username:

If you type a few letters in the above text box, then click the OK button, then the input data is transferred to "html_form_action.php" page. This page displays the results entered.


Try it More examples

Radio button (Radio buttons)
This example demonstrates how to create radio buttons in HTML.

Check boxes (Checkboxes)
This example demonstrates how to create a check box in an HTML page. The user can select or deselect the check box.

Simple drop-down list
This example demonstrates how to create a simple drop-down list box in an HTML page. Drop-down list box is an optional list.

Pre-selection drop-down list
This example demonstrates how to create a simple drop-down list with a preselected value.

Text fields (Textarea)
This example demonstrates how to create a text field (a multi-line text input control). The user can write text in a text field. Writable character of words is not limited.

Create button
This example demonstrates how to create a button. You can text on the button can be customized.

Try it Form instance

Form with border
This example demonstrates how to draw a box around the data with a title.

Input box and confirm the form with buttons
This example demonstrates how to add a form page. This form contains two input fields and a confirmation button.

Form with checkboxes
This form contains two checkboxes, and a confirmation button.

Form with radio buttons
This form contains two boxes and a confirmation button.

Send e-mail from a form
This example demonstrates how to send e-mail from a form.


HTML form tags

New: HTML5 new label

标签 描述
<form> 定义供用户输入的表单
<input> 定义输入域
<textarea> 定义文本域 (一个多行的输入控件)
<label> 定义了 <input> 元素的标签,一般为输入标题
<fieldset> 定义了一组相关的表单元素,并使用外框包含起来
<legend> 定义了 <fieldset> 元素的标题
<select> 定义了下拉选项列表
<optgroup> 定义选项组
<option> 定义下拉列表中的选项
<button> 定义一个点击按钮
<datalist> New 指定一个预先定义的输入控件选项列表
<keygen> New 定义了表单的密钥对生成器字段
<output> New 定义一个计算结果