HTML Forms

HTML Forms are used to collect some data from users on a webpage.

Forms contain special control elements like input text box, check boxes, radio-buttons and submit buttons. Using form elements, you can take different type of data from users. This data can be used for different purposes.

Example of HTML Forms:
You must have seen different kind of forms on websites. For example, User Registration form is used to collect user data to store in a database. Simple Contact form allows user send message or contact website owners. HTML works with Server side programming languages (like PHP or ASP) to send this form into the database or in email.


Form Element

HTML <form> tag is used to defines an HTML form. All other form elements are defined inside this tag. Different types of form elements includes: input text elements, check boxes, radio buttons, submit buttons and many more.

Example of Simple Form Element

Form – Attributes

Form – Attributes List
Attributes Description Syntax & Example
action defines URL of the program or page where action will be performed on form data. action="page2.php"
method specify the HTTP method to send form data. method="get"
target specify the target window or frame target="_blank"
enctype specify the encoding of form data (for media files) enctype="multipart/form-data"

Action Attribute
The action attributes specifies the action to be performed after form submission. Mostly, we pass a page URL into the action.

That action page usually created with some server side programming that can perform some action on form data. Action can be related to inserting form data into database or sending that data into some email. For Example: <form action="page2.php">

Method Attribute
The method attribute tells which HTTP method to use for submitting form data. You can use GET & POST method.

  • The GET method sends for data along with page URL. For Example: <form method="GET">
  • The POST method transfers information via HTTP headers. For Example: <form method="POST">

Target Attribute
The target attribute works in the similar way as in anchor tag. After form submission, action will directed to another page. This action page can be opened in the same window, new window or some frame. Example: <form target="_blank">

  • _blank will open a new page after form is submitted.
  • _self will open action page in the same window.
  • _parent will open action page in the parent frame.

Enctype Attribute
The enctype attribute is used to specify how the browser encodes the form data after submission. Example of Enctype attribute using default values is: <form enctype="application/x-www-form-urlencoded">

When you need to send/upload some media file such as pdf or image, set enctype as a multipart. For example: <form enctype="mutlipart/form-data">

Example of Form with different attributes:


Form – Input Elements

Input element is the most commonly used elements in the form. Input elements are defined by <input> tag. There are many input type elements available in the form. Depending upon the type attribute different input elements can be displayed. Some of the examples of input elements are: text, buttons, check-box and radio-button.

Form – Input type element
Attribute Description Syntax & Example
Text Box used to define text box that allow user to enter some text. input type="text"
Radio Button defines radio button that allow users to select any one option or choice input type="radio"
Checkbox defines checkbox that allow users to select multiple option or choices input type="checkbox"
Button defines normal buttons for users for some action input type="button"
Reset Button defines button that reset form data when user click on it. input type="submit"
Submit Button defines Form submission button that submit form data when user clicks on it input type="reset"

Form – Input Text Field & Attributes

Input Text field defines a single line input field for users. Example: <input type="text">

Attributes: You can use following attributes with text field:

  • type="text" defines the text box/field where user can enter some text.
  • name defines a unique name for every form element to get their data after submission.
  • value specify default or initial value in text field. User can change it..

Radio Button

Radio Button defines radio button where user can select one option from multiple choices in forms. Example: <input type="radio">

Attributes: You can use following attributes with text field:

  • type="radio" defines the Form’s input element as a radio button
  • name defines a unique name for every radio button element to get their data after submission.
  • value The value that will be transferred as data after submission if the radio box is selected.
  • checked It will set current checkbox checked by default.

Checkbox Input

Check Boxes are used when you want to allow the selection of more than one options by user. Example: <input type="check">

Attributes: You can use following attributes with check boxes:

  • type="radio" defines the Form’s input element as a radio button
  • name defines a unique name for every radio button element to get their data after submission.
  • value The value that will be transferred as data after submission if the radio box is selected.
  • checked It will set current checkbox checked by default.

Form – Submit & Reset Button

You can create buttons in Form. Depending upon the type of button, it perform some action on form data.

Type of buttons: There are 3 commonly used buttons in Form:

  • Simple button defines the regular button with no action. We can add some specific action later using JavaScript. Example: <input type="button">
  • Submit button defines the submit button. When user click on submit button, it will submit form to transfer data. Example: <input type="submit">
  • Reset button is used to create reset button. When user click on reset button, all form data will reset to default values. Example: <input type="reset">

Attributes

  • value specify the button value.
  • name defines a unique name for every button to identify later.

Form – Select Menu

Select Element is used to create a drop down list. User can select any item from the list. All list items specified in using option tag.
Select Attributes

  • name defines a unique name for every select menu to identify later get its list data
  • sizescrolling list box
  • multiple attribute allows users to select multiple items form the list at the same time.

Option Attributes

  • value specify the value of list item. After submission, this value will be sent if user selected a item.
  • name defines a unique name for every button to identify later.
  • selected attribute in any option set itself as a default selected item in the drop down list.

Text between option tag is visible for users such as “New Delhi”. Text specified in value is transferred after form submission.


HTML Forms Element – Complete Example

Here is an example of various HTML Form elements. Most of the HTML Form use more than one HTML form elements.

This HTML program will create a Form with different elements. User can fill information using these form elements. After user submit form, all data can be passed into another page. This form data can be processed by some server side programming language such as PHP, ASP or JSP.


Congratulations! Chapter Finished. Do you want to practice more?
Exercises & Assignments
  • No Content Found.
  • Interview FAQs
  • No Content Found.