- Web forms — Working with user data
- Looking to become a front-end web developer?
- Prerequisites
- Introductory guides
- The different form controls
- Form styling guides
- Validating and submitting form data
- Advanced articles
- See also
- Found a content problem with this page?
- HTML Forms
- Example
- The Element
- The Element
- Text Fields
- Example
- The Element
- Radio Buttons
- Example
- Checkboxes
- Example
- The Submit Button
- Example
- Example
Web forms — Working with user data
This module provides a series of articles that will help you master the essentials of web forms. Web forms are a very powerful tool for interacting with users — most commonly they are used for collecting data from users, or allowing them to control a user interface. However, for historical and technical reasons, it’s not always obvious how to use them to their full potential. In the articles listed below, we’ll cover all the essential aspects of Web forms including marking up their HTML structure, styling form controls, validating form data, and submitting data to the server.
Looking to become a front-end web developer?
We have put together a course that includes all the essential information you need to work towards your goal.
Prerequisites
Before starting this module, you should at least work through our Introduction to HTML. At this point you should find the Introductory guides easy to understand, and also be able to make use of our Basic native form controls guide.
Mastering forms however requires more than just HTML knowledge — you also need to learn some specific techniques to style form controls, and some scripting knowledge is required to handle things like validation and creating custom form controls. Therefore, before you look at the other sections listed below we’d recommend that you go away and learn some CSS and JavaScript first.
The above text is a good indicator as to why we’ve put web forms into its own standalone module, rather than trying to mix bits of it into the HTML, CSS, and JavaScript topic areas — form elements are more complex than most other HTML elements, and they also require a close marriage of related CSS and JavaScript techniques to get the most out of them.
Note: If you are working on a computer/tablet/other device where you don’t have the ability to create your own files, you could try out (most of) the code examples in an online coding program such as JSBin or Glitch.
Introductory guides
The first article in our series provides your very first experience of creating a web form, including designing a simple form, implementing it using the right HTML elements, adding some very simple styling via CSS, and how data is sent to a server.
With the basics out of the way, we now look in more detail at the elements used to provide structure and meaning to the different parts of a form.
The different form controls
We start off this section by looking at the functionality of the original HTML types in detail, looking at what options are available to collect different types of data.
Here we continue our deep dive into the element, looking at the additional input types provided when HTML5 was released, and the various UI controls and data collection enhancements they provide. Additionally, we look at the element.
Next we take a look at all the non- form controls and associated tools, such as , , , and .
Form styling guides
This article provides an introduction to styling forms with CSS, including all the basics you might need to know for basic styling tasks.
Here we look at some more advanced form styling techniques that need to be used when trying to deal with some of the more difficult-to-style form elements.
An introduction to the UI pseudo-classes enabling HTML form controls to be targeted based on their current state.
Validating and submitting form data
Sending data is not enough — we also need to make sure that the data users enter into forms is in the correct format to process it successfully, and that it won’t break our applications. We also want to help our users to fill out our forms correctly and not get frustrated when trying to use our apps. Form validation helps us achieve these goals — this article tells you what you need to know.
This article looks at what happens when a user submits a form — where does the data go, and how do we handle it when it gets there? We also look at some of the security concerns associated with sending form data.
Advanced articles
The following articles aren’t essential to the learning pathway, but they’ll prove interesting and useful when you’ve mastered the above techniques and want to know more.
You’ll come across some cases where the native form widgets just don’t provide what you need, e.g. because of styling or functionality. In such cases, you may need to build your own form widget out of raw HTML. This article explains how you’d do this and the considerations you need to be aware of when doing so, with a practical case study.
This article looks at ways to use a form to assemble an HTTP request and send it via custom JavaScript, rather than standard form submission. It also looks at why you’d want to do this, and the implications of doing so. (See also Using FormData objects.)
This last article provides a handy reference allowing you to look up what CSS properties are compatible with what form elements.
See also
Found a content problem with this page?
This page was last modified on Jul 3, 2023 by MDN contributors.
HTML Forms
An HTML form is used to collect user input. The user input is most often sent to a server for processing.
Example
The Element
The HTML element is used to create an HTML form for user input:
The element is a container for different types of input elements, such as: text fields, checkboxes, radio buttons, submit buttons, etc.
All the different form elements are covered in this chapter: HTML Form Elements.
The Element
The HTML element is the most used form element.
An element can be displayed in many ways, depending on the type attribute.
Type | Description |
---|---|
Displays a single-line text input field | |
Displays a radio button (for selecting one of many choices) | |
Displays a checkbox (for selecting zero or more of many choices) | |
Displays a submit button (for submitting the form) | |
Displays a clickable button |
All the different input types are covered in this chapter: HTML Input Types.
Text Fields
The defines a single-line input field for text input.
Example
A form with input fields for text:
This is how the HTML code above will be displayed in a browser:
Note: The form itself is not visible. Also note that the default width of an input field is 20 characters.
The Element
Notice the use of the element in the example above.
The tag defines a label for many form elements.
The element is useful for screen-reader users, because the screen-reader will read out loud the label when the user focuses on the input element.
The element also helps users who have difficulty clicking on very small regions (such as radio buttons or checkboxes) — because when the user clicks the text within the element, it toggles the radio button/checkbox.
The for attribute of the tag should be equal to the id attribute of the element to bind them together.
Radio Buttons
The defines a radio button.
Radio buttons let a user select ONE of a limited number of choices.
Example
A form with radio buttons:
Choose your favorite Web language:
This is how the HTML code above will be displayed in a browser:
Choose your favorite Web language:
Checkboxes
The defines a checkbox.
Checkboxes let a user select ZERO or MORE options of a limited number of choices.
Example
This is how the HTML code above will be displayed in a browser:
I have a bike
I have a car
I have a boat
The Submit Button
The defines a button for submitting the form data to a form-handler.
The form-handler is typically a file on the server with a script for processing input data.
The form-handler is specified in the form’s action attribute.
Example
A form with a submit button:
This is how the HTML code above will be displayed in a browser:
The Name Attribute for
Notice that each input field must have a name attribute to be submitted.
If the name attribute is omitted, the value of the input field will not be sent at all.
Example
This example will not submit the value of the «First name» input field: