What is a form?
Forms collect information from visitors: sign-ups, logins, contact messages and search boxes. The form sends data to a server, where PHP, Node.js or another language processes it.
A simple form
<form action="/submit" method="post">
<label for="name">Name</label>
<input type="text" id="name" name="name" required>
<label for="email">Email</label>
<input type="email" id="email" name="email" required>
<label for="msg">Message</label>
<textarea id="msg" name="message" rows="4"></textarea>
<button type="submit">Send</button>
</form>Key attributes
actionis where the data is sent.methodisget(data in the URL) orpost(data in the request body). Usepostfor anything private.nameis the key under which the value is sent. Without it, the field is not submitted.<label for="id">links a label to its input and makes the field easier to tap.requiredmakes the browser check that the field is filled.
Common input types
<input type="password" name="pwd">
<input type="number" name="age" min="1" max="100">
<input type="date" name="dob">
<input type="checkbox" name="agree" value="yes">
<input type="radio" name="level" value="beginner">
<input type="file" name="resume">Select and options
<select name="course">
<option value="html">HTML</option>
<option value="css">CSS</option>
</select>Note: Browser checks such as required are for convenience only. Always validate again on the server, because anyone can bypass the browser.
Practice
Build a registration form with name, email, a password, a course dropdown and a checkbox to accept terms.
Practice tasks
Reading is not enough. Try these without copying code, then compare with the lesson.
- Create a sign-up form with name, email, password and a submit button, each with a label.
- Add required and a pattern attribute for validation.
- Add radio buttons for a choice and a select list for a city.
Common mistakes
- Inputs without labels or with labels not connected via for and id.
- Relying only on browser validation and forgetting server-side checks.
Quick check
Why use a label element?
It describes the input and enlarges the click area.
What does the name attribute do?
It becomes the key of the value when the form is submitted.