Score code, devs, and debt fast.

Start free trial
HTML

HTML

HTML Forms

HTML forms let users send data to your server. You can use forms to sign up, send feedback, or complete a purchase. This tutorial covers how to build forms, work with key elements and attributes, and follow practical examples.

Learning Outcomes

After reading this tutorial, you will be able to:

  • Describe how HTML forms collect user input
  • Create basic forms using <input>, <label>, and <textarea>
  • Set action, method, and enctype to control form submissions
  • Build forms with checkboxes, radio buttons, and select menus
  • Understand the role of form validation and security when submitting user data

The <form> Element

The <form> element in HTML is used to create an interactive form that collects user input. It acts as a container for various input controls such as text fields, buttons, checkboxes, and more.

Syntax

<form action="URL" method="POST">
  <!-- form elements go here -->
</form>

The <form> tag typically includes the action attribute to define where the data will be sent after submission, and the method attribute to determine the HTTP method (GET or POST) for data transmission.

  • action defines the URL where the form data is sent.
  • method specifies the HTTP method used for data transmission (GET or POST).

Form Attributes

Form attributes allow you to control how form data is submitted and how the form behaves.

Attribute Description
action Specifies the URL to which the form data will be submitted.
method Defines the HTTP method (GET or POST) to be used when submitting the form.
target Specifies where to display the response after form submission. For example, target="_blank" opens the response in a new window or tab.
enctype Defines how form data is encoded when sending to the server (used with POST method).
novalidate Skips form validation before submitting.
autocomplete Allows the browser to autofill form fields based on previously entered data.
  • method: Use GET for simple queries and POST for sensitive or large data.
  • enctype: Required when sending data like files via POST.
  • target: Controls where to show the form submission result.

Essential Form Elements

Forms in HTML consist of various interactive controls, known as form elements. Each element is used to capture specific types of data.

Element Description Example
<input> Collects user input, such as text, password, and email. <input type="text">
<textarea> Defines a multiline text input. <textarea rows="4" cols="50"></textarea>
<button> Defines a clickable button to submit or control actions. <button type="submit">Submit</button>
<select> Creates a drop-down menu for user selection. <select><option>Option 1</option></select>
<label> Defines a label for an input element. <label for="email">Email:</label>
<fieldset> Groups related elements within a form. <fieldset><legend>Personal Info</legend></fieldset>
<legend> Specifies a caption for the <fieldset> element. <legend>Personal Info</legend>
<datalist> Provides a list of predefined options for an input element. <datalist><option value="Apple"></option></datalist>
<output> Displays the result of a calculation or action. <output name="result"></output>
<option> Defines a selectable option in a drop-down list. <option value="apple">Apple</option>
  • <label>: Always associate labels with input fields to improve accessibility.
  • <fieldset>: Helps group related form controls together for better readability.

The <input> Element

The <input> element is one of the most commonly used form elements. It allows you to capture various types of user input by specifying different type attributes.

Input Type Description Example
text A single-line text input. <input type="text" name="username">
password A password field that hides user input. <input type="password" name="password">
email Validates that the user enters a valid email address. <input type="email" name="email">
radio Radio buttons that allow a single selection from multiple options. <input type="radio" name="gender" value="male">
checkbox Checkboxes for selecting multiple options. <input type="checkbox" name="subscribe">
submit A button that submits the form data. <input type="submit" value="Submit">
  • type="email": Validates user input for a correct email format.
  • type="password": Use when sensitive data (like passwords) is involved.

Why Use HTML Forms?

HTML forms are essential for interactive websites. Use forms to collect user data for:

  • User Registration. Collect usernames, passwords, and email addresses.
  • Surveys or Feedback. Get valuable insights from users.
  • File Uploads. Allow users to submit images, documents, or resumes.

Creating Basic HTML Forms

Here’s an example of a simple registration form:

<form action="/submit_form" method="POST">
  <label for="name">Name:</label>
  <input type="text" id="name" name="name" required>

  <label for="email">Email:</label>
  <input type="email" id="email" name="email" required>

  <label for="password">Password:</label>
  <input type="password" id="password" name="password" required>

  <input type="submit" value="Submit">
</form>
  • action: Specifies where the form data is sent (/submit_form).
  • method: POST is used to submit sensitive data securely.
  • required: Ensures that users must fill out the fields before submission.

Output:

Creating Advanced HTML Forms

For more complex forms, you might need additional form elements. Here’s an example:

<form action="/submit" method="POST">
  <label for="name">Full Name:</label>
  <input type="text" id="name" name="name">

  <label>Gender:</label>
  <input type="radio" id="male" name="gender" value="male">
  <label for="male">Male</label>
  <input type="radio" id="female" name="gender" value="female">
  <label for="female">Female</label>

  <label for="country">Select Country:</label>
  <select name="country" id="country">
    <option value="USA">USA</option>
    <option value="UK">UK</option>
    <option value="India">India</option>
  </select>

  <label for="message">Message:</label>
  <textarea name="message" id="message" rows="4" cols="50"></textarea>

  <input type="checkbox" id="newsletter" name="newsletter">
  <label for="newsletter">Subscribe to newsletter</label>

  <input type="submit" value="Submit">
</form>
  • Radio Buttons. Grouped by the same name to allow a single selection.
  • Select Dropdown. Lets users choose from predefined options.
  • Checkboxes. Useful for allowing multiple selections, like subscribing to a newsletter.

Output:

Form Validation and Security

Basic Validation

Use HTML5 attributes like required, minlength, and pattern to ensure user input is valid before submission.

Example of a required field:

<input type="text" name="username" required>

Security Considerations

  • Always use POST for forms handling sensitive data (such as passwords).
  • Make sure your form submits over HTTPS to ensure the data is securely transmitted.

FAQs on HTML Forms

What Is the Advantage of Using HTML Forms?

HTML forms enable websites to interact with users, allowing them to submit data for various purposes like registration, feedback, or orders. Forms are essential for gathering and processing information from users in a structured way.

How to Create a Drop-Down in an HTML Form?

To create a drop-down menu in a form, use the <select> element, with multiple <option> elements to define the choices.

<label for="country">Select a Country:</label>
<select id="country" name="country">
  <option value="USA">USA</option>
  <option value="India">India</option>
  <option value="UK">UK</option>
</select>

When Should You Use an HTML Form?

Use an HTML form when you need user input. For example:

  • Register new users
  • Collect feedback
  • Process purchases
  • Handle other data entry tasks
Subscribe to our newsletter

Get the freshest news and resources for developers, designers and digital creators in your inbox each week

© 2000 – 2025 SitePoint Pty. Ltd.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.