HTML Forms

HTML Forms
❮ Previous Next ❯
HTML Form Example
First name:

Mickey

Last name:

Mouse

Submit
»
The Element
The HTML element defines a form that is used to collect user input:

.
form elements
.

An HTML form contains form elements.

Form elements are different types of input elements, like text fields, checkboxes, radio buttons, submit buttons, and more.

The Element
The element is the most important form element.

The element can be displayed in several ways, depending on the type attribute.

Here are some examples:

Type Description
Defines a one-line text input field
Defines a radio button (for selecting one of many choices)
Defines a submit button (for submitting the form)
You will learn a lot more about input types later in this tutorial.

Text Input
defines a one-line input field for text input:

Example

  First name:

 

  Last name:

 

»
This is how it will look like in a browser:

First name:

Last name:

Note: The form itself is not visible. Also note that the default width of a text field is 20 characters.

Radio Button Input
defines a radio button.

Radio buttons let a user select ONE of a limited number of choices:

Example

  Male

  Female

  Other

»
This is how the HTML code above will be displayed in a browser:

 Male
 Female
 Other
The Submit Button
defines a button for submitting the form data to a form-handler.

The form-handler is typically a server page with a script for processing input data.

The form-handler is specified in the form’s action attribute:

Example

  First name:

 

  Last name:

 

 

»
This is how the HTML code above will be displayed in a browser:

First name:

Mickey

Last name:

Mouse

Submit
The Action Attribute
The action attribute defines the action to be performed when the form is submitted.

Normally, the form data is sent to a web page on the server when the user clicks on the submit button.

In the example above, the form data is sent to a page on the server called “/action_page.php”. This page contains a server-side script that handles the form data:

If the action attribute is omitted, the action is set to the current page.

The Method Attribute
The method attribute specifies the HTTP method (GET or POST) to be used when submitting the form data:

or:

When to Use GET?
The default method when submitting form data is GET.

However, when GET is used, the submitted form data will be visible in the page address field:

/action_page.php?firstname=Mickey&lastname=Mouse
Note: GET must NOT be used when sending sensitive information! GET is best suited for short, non-sensitive, amounts of data, because it has size limitations too.

When to Use POST?
Always use POST if the form data contains sensitive or personal information. The POST method does not display the submitted form data in the page address field.

POST has no size limitations, and can be used to send large amounts of data.

The Name Attribute
Each input field must have a name  attribute to be submitted.

If the name attribute is omitted, the data of that input field will not be sent at all.

This example will only submit the “Last name” input field:

Example

  First name:

 

  Last name:

 

 

»
Grouping Form Data with
The element is used to group related data in a form.

The element defines a caption for the element.

Example

 
    Personal information:
    First name:

   

    Last name:

   

   
 

»
This is how the HTML code above will be displayed in a browser:

Personal information:
First name:

Mickey

Last name:

Mouse

Submit
More Examples
Send e-mail from a form
How to send e-mail from a form.

Test Yourself with Exercises!
Exercise 1 »   Exercise 2 »   Exercise 3 »   Exercise 4 »

Here is the list of attributes:

Attribute Description
accept-charset Specifies the charset used in the submitted form (default: the page charset).
action Specifies an address (url) where to submit the form (default: the submitting page).
autocomplete Specifies if the browser should autocomplete the form (default: on).
enctype Specifies the encoding of the submitted data (default: is url-encoded).
method Specifies the HTTP method used when submitting the form (default: GET).
name Specifies a name used to identify the form (for DOM usage: document.forms.name).
novalidate Specifies that the browser should not validate the form.
target Specifies the target of the address in the action attribute (default: _self).
You will learn more about the form attributes in the next chapters.

❮ Previous Next ❯

Leave a comment