Part 12: Understanding HTML Forms Basics
Senior Full-Stack Developer
HTML Forms are used to collect user input and send it to a server for processing.
The <form> element is the container for all form elements.
action and method attributesForms are used in virtually every interactive website:
Specifies where to send the form data
Can be a server URL or local file
Essential for form functionality
action="/submit-form.php"
Appends data to URL (visible)
Sends data in request body (hidden)
GET shows data, POST hides it
POST is more secure for sensitive data
<form action="/login.php" method="POST">
<!-- Form elements go here -->
</form>
search.php?query=html&category=tutorials
<form action="/login" method="POST">
Single-line text field
<input type="text" name="username">
Masked text for passwords
<input type="password" name="password">
Email address validation
<input type="email" name="email">
Numeric input with controls
<input type="number" name="age">
Date picker interface
<input type="date" name="birthday">
Multiple selection
<input type="checkbox" name="interests">
Single selection from options
<input type="radio" name="gender">
Multi-line text input
<textarea name="message"></textarea>
name - Identifies the field when submitting
placeholder - Hint text inside the field
required - Makes the field mandatory
value - Default value for the field
<!DOCTYPE html>
<html>
<head>
<title>Login Form</title>
</head>
<body>
<h2>User Login</h2>
<form action="/login" method="POST">
<div class="form-group">
<label for="username">Username:</label>
<input type="text" id="username" name="username"
placeholder="Enter your username" required>
</div>
<div class="form-group">
<label for="password">Password:</label>
<input type="password" id="password" name="password"
placeholder="Enter your password" required>
</div>
<div class="form-group">
<input type="checkbox" id="remember" name="remember">
<label for="remember">Remember me</label>
</div>
<button type="submit">Login</button>
</form>
</body>
</html>
Provides text description for form controls. The for attribute should match the input's id.
required makes the field mandatory. placeholder shows hint text.
type="submit" creates a button that submits the form data.
Always associate labels with form controls using the for attribute
Ensure forms work well on mobile devices with appropriate input types
Ensure all form elements are accessible via keyboard
Use descriptive labels that clearly indicate what information is needed
Arrange fields in a logical sequence that matches user expectations
Use HTML5 validation attributes like required, pattern
Always use POST method for passwords, personal information, and payments
Never rely solely on client-side validation
Use specific input types (email, tel, url) for better validation and mobile experience
You've learned HTML Forms Basics
Purpose and usage of forms
Action and method attributes
GET vs POST comparison
Basic form input elements
Built a practical login form
Form accessibility and usability
Create an HTML registration form with:
Next: Learn about advanced input types, form validation, and form styling