HTML Forms

Table of Content

Introduction to HTML Forms

What is an HTML Form?

An HTML form is like an online questionnaire that allows users to enter information into a website. It's a way for websites to collect user input, such as their name, email, or preferences.

Purpose and Usage of Forms

Forms have many purposes:

  • Collecting user data: For example, a contact form on a website can collect user information like name, email, and message.
  • Creating user accounts: Websites can use forms to allow users to register and create accounts.
  • Processing orders: Online shopping websites use forms for users to enter their payment and shipping details.
  • Scheduling appointments: Forms can allow users to book appointments with doctors or other professionals.
  • Submitting feedback: Websites often have feedback forms where users can share their opinions and suggestions.

Code Snippet: Creating a Basic HTML Form

Here's a simple code snippet to create a basic HTML form:

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

  <input type="email" name="email">

  <input type="submit" value="Submit">


  • <form> tag creates the form.
  • label tags define the labels for the form fields.
  • input tags create the form fields.
  • type attribute of input tags specifies the type of field (e.g., text, email, submit).
  • name attribute of input tags gives a unique name to each field.
  • submit button submits the form.

How it Works:

When a user enters information into the form fields and clicks the submit button, the data is sent to the server (where the website is hosted). The server then processes the data and responds accordingly, such as sending a confirmation email or storing the user's information in a database.

Understanding Input Elements

Types of Input Elements in HTML

Input elements in HTML are used to collect user input. There are different types of input elements for different kinds of data.

Common Input Elements:

  • Text: Used for entering text, such as names or addresses.
  • Password: Used for entering sensitive information that should be hidden, such as passwords.
  • Checkbox: Used to indicate a yes or no answer.
  • Radio button: Used to select one option from a group of options.
  • Submit button: Triggers the submission of a form.
  • Reset button: Resets the form to its original state.

HTML Code for Different Input Types:

<!-- Text input -->
<label for="name">Name:</label>
<input type="text" id="name" placeholder="Enter your name">

<!-- Password input -->
<label for="password">Password:</label>
<input type="password" id="password" placeholder="Enter your password">

<!-- Checkbox -->
<label for="agree">Do you agree?</label>
<input type="checkbox" id="agree">

<!-- Radio button -->
<label for="gender">Gender:</label>
<input type="radio" id="male" name="gender" value="male">
<label for="female">Female</label>
<input type="radio" id="female" name="gender" value="female">

<!-- Submit button -->
<input type="submit" value="Submit">

<!-- Reset button -->
<input type="reset" value="Reset">

Example: Input Element with Label and Placeholder

<label for="email">Email:</label>
<input type="email" id="email" placeholder="">
  • Label: This provides a label for the input element, making it clear to the user what kind of data is being asked for.
  • Placeholder: This provides a hint or example of what type of data should be entered in the input element.

Form Actions and Handling User Input

What is Form Action?

Imagine a form on a website. When you fill in the form and click "Submit," that form needs to know where to send the information you entered. That's where "form action" comes in. It tells the form where to "go" when submitted.

Understanding the Code Snippet:

<form action="process_data.php">
  • <form>: Starts the form on the web page.
  • action="process_data.php": Specifies where the form should send the information (in this case, a PHP file named "process_data.php").


Let's say you have a form for taking orders on a website. When someone fills out the form and clicks "Submit," the form will send the order information to the "process_data.php" file. That PHP file will then process the data and save the order to the database.


Form action is important because it tells the browser where to send the submitted data. Without it, the data would not be sent anywhere and the form would not work correctly.

Form Validation: Ensuring Data Quality

Importance of Form Validation

When people fill out online forms, they expect their information to be submitted correctly. Form validation helps ensure that the data entered is complete, accurate, and in the correct format.

Basic Validation Using HTML5 Input Attributes

HTML5 has built-in input attributes that can perform basic validation:

  • required: Ensures that a field is not empty.
  • min: Specifies the minimum value allowed.
  • max: Sets the maximum value allowed.
  • pattern: Validates the entered value against a regular expression (a rule for text patterns).

Example: Using Required Attribute

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

This creates a text field where the user's name is required. If the user tries to submit the form without entering a name, the browser will display an error.

Form Submission and Data Handling

Form Submit Button

When you click a "Submit" button on a web form, it sends the information you entered into the form to a server.


This is a special way of sending form data that allows us to include both text and files. It's like a bag that can hold different items.

Handling Form Data on the Server

When the server receives the form data, it needs to extract the information. Code snippets written in a programming language help the server do this.

Creating an Email Form

HTML Code for an Email Form

This code creates a simple form that asks for an email address and then checks if the address is valid.

<!DOCTYPE html>
  <title>Contact Form</title>
  <h1>Contact Us</h1>
    <label for="email">Email Address:</label>
    <input type="email" id="email" name="email">
    <input type="submit" value="Submit">


  • The <html>, <head>, and <body> tags define the basic structure of the web page.
  • The <title> tag sets the title of the webpage, which appears in the browser's title bar.
  • The <h1> tag creates a heading with the text "Contact Us".
  • The <form> tag defines a form where users can enter their email address.
  • The <label> tag defines a label for the email address input field.
  • The <input> tag creates an input field where users can type their email address. The type="email" attribute specifies that the input field should accept email addresses. The id="email" and name="email" attributes are used to identify the input field.
  • The <input> tag creates a submit button with the value "Submit". When clicked, this button will submit the form.


The email input field uses the type="email" attribute to enable browser-based validation. This means that most browsers will automatically check if the entered value is a valid email address and provide visual feedback (e.g., highlighting errors or displaying a validation message).

Example Usage

To use this form, you can copy the HTML code into a text editor and save it as a file with a .html extension, e.g., contact-form.html. Then, open the file in a web browser like Chrome or Firefox. You should see a form with a text field for entering your email address and a submit button. Enter your email address and click the "Submit" button. If your email address is valid, the form will be submitted successfully. Otherwise, the browser will display an error message.

Common Mistakes to Avoid in Form Design:

  • Using too many fields: Keep the number of fields to a minimum.
  • Asking for unnecessary information: Only ask for information that is absolutely necessary.
  • Using confusing language: Use clear and concise language that is easy to understand.
  • Using dropdown menus with too many options: Limit the number of options in dropdown menus to a manageable amount.
  • Placing fields in a logical order: Arrange fields in a way that makes sense to users.

Tips for Accessibility and Cross-Browser Compatibility:

  • Use labels for all fields: This helps users with screen readers understand what each field is for.
  • Provide clear error messages: When a user enters invalid information, explain the error in a helpful way.
  • Test your forms on multiple browsers: Make sure your forms work as intended in as many browsers as possible.