HTML forms and input
HTML forms are used to collect different types of user input.
The HTML form represents an area in the document that contains interactive controls that send the information collected by the user to the Web server.
HTML form
A form is an area that contains form elements.
Form elements allow users to enter content in the form, such as text fields (textarea), drop-down lists, radio-buttons boxes, checkboxes, and so on.
Forms use form tags <form>
to set up:
Example
<form>
.
*input element*
.
</form>
HTML form-input element
In most cases, the form tag used is the input tag (< input >).
The input type is defined by the type type. Most of the frequently used input types are as follows:
Text field
The text field is set by the < input type= “text” > tag, which is used when users type letters, numbers, and so on into the form.
<form>
First name: <input type="text" name="firstname"><br>
Last name: <input type="text" name="lastname">
</form>
The browser displays as follows:
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
The password field is defined by the label < input type= “password” >:
<form>
Password: <input type="password" name="pwd">
</form>
The browser displays as follows:
Password:
Note: password field characters are not displayed in clear text, but are replaced by asterisks or dots.
Check box
< input type= “checkbox” > defines a check box. The user needs to select one or more options from several given selections.
<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 as follows:
I have a bike
I have a car
HTML form label
New: new HTML5 tag
Label |
Description |
---|---|
< form > |
Define a form for user input |
< input > |
Define input field |
< textarea > |
Define a text field (a multiline input control) |
< label > |
Defines the tag of the < input > element, which is usually the input title |
< fieldset > |
Defines a set of related form elements and contains them using a bounding box |
< legend > |
Defines the title of the < fieldset > element |
< select > |
Defines a list of drop-down options |
< optgroup > |
Define option groups |
< option > |
Define options in the drop-down list |
< button > |
Define a click button |
< datalist > New |
Specify a predefined list of input control options |
< keygen > New |
Defines the key pair generator field for the form |
< output > New |
Define a calculation result |