HTML form Tag: Step-by-Step Guide (With Examples)
When you browse the internet, you often come across forms—login pages, sign-up forms, contact forms, or survey questionnaires. All these are powered by the HTML <form>
tag.
The <form>
tag is one of the most important elements in web development because it allows users to input data and send it to a server for processing. Without forms, websites could not collect information such as usernames, passwords, or feedback.
In this guide, you’ll learn:
What the
<form>
tag is and why it is requiredThe attributes of
<form>
How to create an HTML form step by step
A complete working example
What is the HTML form Tag?
The <form>
tag in HTML is used to create a section where users can enter data. This data is then sent to a server using either GET or POST methods.
In simple words:
👉 A form is like a box where people write their information, and the browser sends it to the website for processing.
Why is the form Tag Required?
Forms are essential in modern websites because they:
Collect user data (e.g., registration, login, surveys).
Enable interaction (e.g., search bars, file uploads, order forms).
Send data to the server for processing (e.g., storing information in a database).
Provide accessibility so users can interact with a website beyond just reading.
Without <form>
, websites would only be static and could not interact with users.
Syntax of form Tag
<form action="URL" method="get|post">
<!-- Form elements go here -->
</form>
Important Attributes:
action – Defines where the form data should be sent (URL of server-side script like PHP, Python, etc.).
method – Defines how the data is sent:
GET
: Appends data to the URL (good for searches).POST
: Sends data securely in the background (good for sensitive data like passwords).
name – Gives a name to the form (useful in JavaScript).
target – Defines where to display the response (e.g.,
_blank
for new tab).
Step-by-Step Guide to Create a Form
Step 1: Start with the <form>
Tag
<form action="/submit.php" method="post">
</form>
Here:
action="/submit.php"
→ form data will be sent to a file namedsubmit.php
.method="post"
→ data will be sent securely.
Step 2: Add Input Fields
Forms usually have text boxes, passwords, and checkboxes.
<input type="text" name="username" placeholder="Enter your username">
<input type="password" name="password" placeholder="Enter your password">
Step 3: Add Labels for Accessibility
Labels help users and screen readers understand what each input means.
<label for="username">Username:</label>
<input type="text" id="username" name="username">
Step 4: Add Other Form Elements
Forms can include many types of inputs:
Radio buttons
Checkboxes
Dropdown menus
Text areas
Example:
<label for="gender">Gender:</label>
<input type="radio" name="gender" value="male"> Male
<input type="radio" name="gender" value="female"> Female
Step 5: Add a Submit Button
Every form needs a button to send the data.
<input type="submit" value="Submit">
Complete Example of HTML Form
Here’s a working example of a simple login form:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>HTML Form Example</title>
</head>
<body>
<h2>Login Form</h2>
<form action="/submit.php" method="post">
<label for="username">Username:</label>
<input type="text" id="username" name="username" placeholder="Enter Username" required>
<br><br>
<label for="password">Password:</label>
<input type="password" id="password" name="password" placeholder="Enter Password" required>
<br><br>
<label>Remember Me:</label>
<input type="checkbox" name="remember">
<br><br>
<input type="submit" value="Login">
</form>
</body>
</html>
How This Form Works:
The user enters a username and password.
They can tick the “Remember Me” checkbox.
When they click Login, the data is sent to
/submit.php
using the POST method.
Best Practices for Using form:
Always use
label
for accessibility.Use
required
attribute to ensure important fields are filled.Use
method="post"
for sensitive data.Always validate form data on both client-side (JavaScript) and server-side (PHP, Python, etc.).