HTML Forms and User Input
From the IT EXAM curriculum
TL;DR
HTML forms are how you collect information from users on a website, using various input types like text boxes and checkboxes. When a user submits a form, their data is sent to a server for processing. Understanding how to structure forms and handle their submission is crucial for interactive web pages.
1. The Mental Model
Think of an HTML form as a digital questionnaire. You fill out the fields, and when you hit "submit," all your answers are bundled up and sent to another program (usually on a server) that knows what to do with them.
2. The Core Material
HTML forms are built using the <form> tag, which acts as a container for all your input elements. Inside this container, you'll place different types of controls to gather various kinds of data.
The <form> Tag Attributes

Photo by Pixabay on Pexels
The <form> tag has two really important attributes:
* action: This specifies where the form data should be sent when submitted. It's usually a URL pointing to a server-side script (e.g., a PHP, Python, or Node.js file).
* method: This defines how the data is sent. The two most common methods are GET and POST.
* GET: Appends form data to the URL as query parameters (e.g., www.example.com/process?name=Alice&age=30). It's good for non-sensitive data or when you want users to bookmark the result.
* POST: Sends form data in the body of the HTTP request. This is better for sensitive information (like passwords) or large amounts of data, as it's not visible in the URL.
Common Input Types

Photo by Max DeRoin on Pexels
Input elements are created using the <input> tag, which has a type attribute that dictates its appearance and behavior. Each input also needs a name attribute, which is how the server identifies the data submitted from that field.
Here are some common types:
* text: For single-line text input (e.g., name, email).
* password: Similar to text, but masks the input for security.
* submit: A button that submits the form.
* checkbox: For selecting zero or more options from a list.
* radio: For selecting exactly one option from a group (they need the same name to be grouped).
* email: For email addresses; browsers might add basic validation.
* number: For numeric input; browsers might add spin buttons.
* date: For selecting a date.
Other useful form elements:
* <textarea>: For multi-line text input (e.g., comments).
* <select>: For drop-down lists, with <option> tags inside for each choice.
* <label>: Associates text with a form control. Clicking the label focuses the input. Use the for attribute on the label, matching the id of the input.
Form Submission Process

Photo by RDNE Stock project on Pexels
Here's how data typically flows when a user interacts with a form:
graph TD
A["User Fills Out Form"] --> B["Clicks 'Submit' Button"];
B --> C{Form `method`?};
C -- "GET" --> D["Data Appended to URL (Query String)"];
C -- "POST" --> E["Data Sent in Request Body"];
D --> F["Browser Sends HTTP Request to `action` URL"];
E --> F;
F --> G["Server Receives Request"];
G --> H["Server-side Script Processes Data"];
H --> I["Server Sends Response (e.g., new page, confirmation)"];
I --> J["Browser Displays Response"];
3. Worked Example
Let's create a simple contact form.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Contact Us</title>
</head>
<body>
<h1>Contact Our Team</h1>
<form action="/submit-contact" method="post">
<div>
<label for="fullName">Your Name:</label><br>
<input type="text" id="fullName" name="user_name" required>
</div>
<br>
<div>
<label for="userEmail">Your Email:</label><br>
<input type="email" id="userEmail" name="user_email" required>
</div>
<br>
<div>
<label for="messageText">Your Message:</label><br>
<textarea id="messageText" name="user_message" rows="5" cols="30" required></textarea>
</div>
<br>
<div>
<input type="checkbox" id="newsletter" name="subscribe_newsletter" value="yes">
<label for="newsletter">Subscribe to our newsletter?</label>
</div>
<br>
<div>
<input type="submit" value="Send Message">
</div>
</form>
</body>
</html>
In this example:
* The action is /submit-contact and the method is post. This means when the "Send Message" button is clicked, the browser will send the data to a server-side script at /submit-contact using the POST method.
* We have text, email, textarea, checkbox, and submit input types.
* Each input has a unique id and a name. The name is what the server will actually use (e.g., user_name will be the key for the submitted name value).
* The for attribute on each <label> matches an id on an input, making the form more accessible.
* required means the user must fill out that field before submitting.
If you filled this out and clicked submit, the server script at /submit-contact would receive data that looks something like this (conceptually, depending on server language):
{
"user_name": "Jane Doe",
"user_email": "jane.doe@example.com",
"user_message": "Hello, I have a question about...",
"subscribe_newsletter": "yes" // or absent if unchecked
}
4. Key Takeaways
- Forms use the
<form>tag as a container for all inputs. - The
actionattribute tells the browser where to send form data. - The
methodattribute (GETorPOST) dictates how data is sent. <input>tags are versatile; theirtypeattribute changes their behavior (e.g.,text,password,submit,checkbox).- Every input needing to send data must have a
nameattribute. <label>tags improve accessibility by linking text to form controls.- Use
POSTfor sensitive or large data, andGETfor simple queries or bookmarkable results.
Common Mistakes to Avoid

Photo by KATRIN BOLOVTSOVA on Pexels
- Forgetting the
nameattribute on input fields; without it, data won't be sent to the server. - Not matching
<label for="">with<input id="">, which harms accessibility. - Using
GETfor sensitive data like passwords, as it exposes information in the URL. - Not including a
submitinput type, making it hard for users to send the form. - Having radio buttons in a group with different
nameattributes; they won't function as a single-choice group.
5. Now Try It
Create a registration form. It should include:
1. A field for "Username" (text).
2. A field for "Password" (password type).
3. A field for "Email" (email type).
4. A dropdown for "Country" with at least three options (e.g., USA, Canada, UK).
5. Two radio buttons for "Gender" (e.g., Male, Female, 'Prefer not to say' — ensure only one can be selected).
6. A "Sign Up" button.
Set the action to /register and method to post. Ensure all relevant fields have name attributes and are associated with label tags.
What success looks like: You'll have an HTML page displaying your form. When you fill it out and click "Sign Up," you should see the URL briefly change (or stay the same if the action points to a non-existent path) but you won't see your data in the URL because you used POST. Inspecting the page's source will show all your correctly structured HTML elements.
Frequently asked about HTML Forms and User Input
More from IT EXAM
Get the full IT EXAM curriculum
Clone the complete plan to your dashboard for unlimited AI-generated notes, practice quizzes, and a personalised revision schedule.
Save this course free