Chapter 5 — HTML Forms & User Input
📚 Chapter Overview
Forms are the primary way users interact with websites. In this chapter, you will learn how to build complete forms using Text inputs, Checkboxes, Radio buttons, and Dropdowns.
Learning Objectives:
- Master the
<form>element and attributes. - Use
type="text",password, andemail. - Understand the
labelandnamerelationship.
5.1 Basic Form Elements
Part 1 — Definition
An HTML form is used to collect user input. The <form> element is a container for different types of input elements. Labels are essential for accessibility, telling the user (and screen readers) what each field is for.
Part 2 — Syntax
<form>
<label for="user">Username:</label>
<input type="text" id="user" name="user">
<input type="submit" value="Send">
</form>
Part 3 — Example
Problem: Create a simple "Contact Us" form.
<form>
<label>Name:</label><br>
<input type="text" placeholder="Enter Name"><br>
<label>Email:</label><br>
<input type="email"><br>
<button type="submit">Submit</button>
</form>
Observation: The placeholder attribute provides a hint inside the input field.
Part 4 — Video
Video: Coming Soon
5.2 Selection Controls
Part 1 — Definition
Checkboxes allow selecting multiple options. Radio buttons allow selecting only one from a group. The <select> tag creates a dropdown list.
Part 2 — Syntax
<input type="checkbox">
<input type="radio" name="group">
<select>
<option>Choice</option>
</select>
Part 3 — Example
Problem: Ask for gender and interests.
<p>Gender:</p>
<input type="radio" name="g"> Male
<input type="radio" name="g"> Female
<p>Interests:</p>
<input type="checkbox"> Coding
<input type="checkbox"> Music
Observation: Radio buttons must have the same name attribute to act as a group.
Part 4 — Video
Video: Coming Soon
🏆 Chapter Challenge
Challenge Objective
Build a "Job Application Form".
Requirements
- Fields for Full Name, Email, and Phone.
- A dropdown for "Position Applied For".
- A file upload for Resume (
type="file"). - A "Terms and Conditions" checkbox.
Step-by-Step Process
- Structure the form using
fieldsetandlegendfor organization. - Add labels to every input.
- Use
<textarea>for "Cover Letter". - Add a Submit button.
Expected Deliverables
apply.htmlsource file.