HTML Forms
Forms are how websites collect information: signing up, logging in, searching, booking a bus seat, applying for a bursary. An HTML form gathers what the user types and chooses, packages it as name–value pairs, and sends it to a server to be processed.
The form element
<form action="/register" method="post"> <label for="name">Full name</label> <input type="text" id="name" name="name"> <label for="town">Town</label> <input type="text" id="town" name="town"> <button type="submit">Register</button> </form>
<form> wraps all the controls that belong together. Two attributes decide where and how the data goes:
actionis the URL that receives the data, usually a page or script on your server. If you leave it out, the form submits to the current page's URL.methodis the HTTP method:get(the default) orpost.
What happens when you press Submit
name and value, sends them to the action URL, and shows the page the server sends back.HTML only handles steps 1 and 2. Receiving and saving the data (step 3) needs server-side code, written for example in Python with Django, or in PHP or Node.js.
GET or POST?
get | post | |
|---|---|---|
| Where the data goes | In the URL's query string | In the body of the request |
| Visible? | Yes: in the address bar, history and server logs | Not in the URL |
| Bookmarkable? | Yes | No |
| Use for | Searches and filters that only read data | Sign-ups, logins, payments, anything that changes data |
POST hides data from the URL but does not encrypt it. Only HTTPS protects data on its way to the server. Never build a login or payment form on a site without HTTPS.
The name attribute is essential
Only controls with a name attribute are sent. The name becomes the key and what the user typed becomes the value, so name="town" with "Kisii" typed in sends town=Kisii. A control without a name is silently left out, a very common beginner bug.
Don't confuse name with id: id connects the input to its <label> and to CSS or JavaScript, while name is what the server receives. Most inputs have both, often with the same value.
The main form controls
<label>: the visible name of a control. Itsforattribute matches the control'sid. Clicking the label focuses the control, and screen readers read it aloud.<input>: a single-line control whose behaviour depends ontype(text, email, password, checkbox and many more; see Input Types).<textarea>: multi-line text, such as a message. Its starting text goes between the tags, not in avalueattribute.<select>with<option>s: a drop-down list. Each option'svalueis what gets sent.<button>: inside a form, a button's default type issubmit. Usetype="button"for buttons that should not submit.
<label for="county">County</label> <select id="county" name="county"> <option value="kisii">Kisii</option> <option value="mombasa">Mombasa</option> <option value="nairobi" selected>Nairobi</option> </select> <label for="message">Message</label> <textarea id="message" name="message" rows="4"></textarea>
The selected attribute makes Nairobi the starting choice. If the user keeps it, the form sends county=nairobi: the option's value, not its visible text.
Try it
This form includes a few lines of JavaScript that stop the real submission (the preview has no server) and instead show the exact data the browser would send. You'll learn how that works in Handling Forms.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Club registration</title>
</head>
<body>
<h1>Join the coding club</h1>
<form action="/register" method="get">
<p>
<label for="name">Full name</label><br>
<input type="text" id="name" name="name">
</p>
<p>
<label for="county">County</label><br>
<select id="county" name="county">
<option value="kisii">Kisii</option>
<option value="mombasa">Mombasa</option>
<option value="nairobi" selected>Nairobi</option>
</select>
</p>
<p>
<label for="why">Why do you want to join?</label><br>
<textarea id="why" name="why" rows="3"></textarea>
</p>
<p><input type="text" placeholder="I have no name, so I am not sent"></p>
<button type="submit">Register</button>
</form>
<p id="result"></p>
<script>
const form = document.querySelector("form");
form.addEventListener("submit", function (event) {
event.preventDefault();
const data = new URLSearchParams(new FormData(form));
document.querySelector("#result").textContent = "The browser would send: " + data.toString();
});
</script>
</body>
</html>
Type "Achieng Moraa" as the name, choose Kisii and press Register. Below the form you'll see The browser would send: name=Achieng+Moraa&county=kisii&why=. Notice three things: spaces are encoded as +, an empty textarea is still sent with an empty value, and the unnamed box is missing entirely.
Key points
<form>wraps controls;actionsays where to send the data andmethodsays how.- GET puts data in the URL (good for searches); POST puts it in the request body (use for anything private or that changes data).
- Only controls with a
nameare submitted, asname=valuepairs. - Use
<label>,<input>,<textarea>,<select>and<button>; a server is needed to process the data.
Tip: use the ← and → keys to move between pages.
