HTML Form

HTML Forms are required when you want to collect some data from the site visitor. For example registration information: name, email address, credit card, etc.

A form will take input from the site visitor and then will post your back-end application such as CGI, ASP Script or PHP script etc. Then your back-end application will do required processing on that data in whatever way you like.
Form elements are like text fields, textarea fields, drop-down menus, radio buttons, checkboxes, etc. which are used to take information from the user.
A simple syntax of using <form> is as follows:

Form Attributes:

  • name: This is the name of the form.
  • action: Here you will specify any script URL which will receive uploaded data.
  • method: Here you will specify the method to be used to upload data. It can take various values but most frequently used are GET and POST.
  • target: It specifies the target page where the result of the script will be displayed. It takes values like _blank, _self, _parent etc.
  • enctype: You can use the enctype attribute to specify how the browser encodes the data before it sends it to the server. Possible values are like:
    • application/x-www-form-urlencoded – This is the standard method most forms use. It converts spaces to the plus sign and non-alphanumeric characters into the hexadecimal code for that character in ASCII text.
    • mutlipart/form-data – This allows the data to be sent in parts, with each consecutive part corresponding the a form control, in the order they appear in the form. Each part can have an optional content-type header of its own indicating the type of data for that form control.

Form controls:

  • Text input controls
  • Buttons
  • Checkboxes and radio buttons
  • Select boxes
  • File select boxes
  • Hidden controls
  • Submit and reset button

1. HTML Forms – Text Input Controls:

There are actually three types of text input used on forms:

  • Single-line text input controls: Used for items that require only one line of user input, such as search boxes or names. They are created using the <input> element.
  • Password input controls: Single-line text input that mask the characters a user enters.
  • Multi-line text input controls: Used when the user is required to give details that may be longer than a single sentence. Multi-line input controls are created with the <textarea> element.

2. HTML Forms – Button:

There are various ways in HTML to create clickable buttons. You can create the clickable button using <input> tag.
When you use the <input> element to create a button, the type of button you create is specified using the type attribute. The type attribute can take the following values:

  • submit: This creates a button that automatically submits a form.
  • reset: This creates a button that automatically resets form controls to their initial values.
  • button: This creates a button that is used to trigger a client-side script when the user clicks that button.

Example:

 

3. Checkbox:

Checkboxes are used when more than one option is required to be selected. They are created using <input> tag as shown below.3. Checkboxes Control:

Example:

 

4. Radio Buttons

Radio Buttons are used when only one option is required to be selected. They are created using <input> tag as shown below:4. Radio box Control:

 

5. Select box Control:

Drop Down Box is used when we have many options available to be selected but only one or two will be selected.

Example:

 

6. File Select Boxes:

If you want to allow a user to upload a file to your web site from his computer, you will need to use a file upload box, also known as a file select box. This is also created using the <input> element.

 

7. Hidden Controls:

If you will want to pass information between pages without the user seeing it. Hidden form controls remain part of any form, but the user cannot see them in the Web browser.

Sample Registration Form in HTML