HTML <form> Tag โ User Input Forms
๐ Introduction
The <form> tag is used to create an interactive form where users can enter and submit data.
It is used in:
- Login pages
- Registration forms
- Contact forms
- Search bars
- Surveys
๐งฑ Syntax
<form>
<!-- input elements -->
</form>
๐ก Example
<!DOCTYPE html>
<html>
<head>
<title>Form Example</title>
</head>
<body>
<h2>Contact Form</h2>
<form>
<label>Name:</label>
<input type="text" />
<br /><br />
<label>Email:</label>
<input type="email" />
<br /><br />
<button type="submit">Send</button>
</form>
</body>
</html>
๐ฅ๏ธ Output Explanation
- Users can type information
- The form collects data
- The submit button sends the data
๐ฏ Important Attributes
1. action
<form action="/submit"></form>
โ Defines where the form sends data
2. method
<form method="POST"></form>
โ Defines how data is sent:
GETโ visible in URLPOSTโ hidden and secure
๐ง Basic Form Structure
A good form usually includes:
<label>โ text description<input>โ user input<button>โ submit
๐งฉ Example with Structure
<form action="/submit" method="POST">
<label>Name:</label>
<input type="text" />
<br />
<label>Password:</label>
<input type="password" />
<br />
<button type="submit">Login</button>
</form>
๐ฏ When to Use <form>
Use it for:
- Login systems
- Registration pages
- Contact pages
- Search functionality
- Data submission
โ ๏ธ Important Notes
- Without
<form>, data cannot be submitted properly - Always use labels for accessibility
- Use correct input types
๐ซ Common Mistakes
- Missing
actionormethodโ - No labels โ
- Wrong input types โ
- Not grouping inputs in form โ
๐จ Styling Example
<form style="padding:10px; border:1px solid #ccc;">
<input type="text" placeholder="Enter name" />
</form>
๐งช Mini Exercise
Create a form with:
- Name input
- Email input
- Password input
- Submit button
- Use labels for each input