Forms and User Input
🎯 Introduction
In web development, forms connect users with websites. Entering a shipping address, typing a password to sign in, or searching with keywords all involve forms.
Learning HTML forms lets you:
- Collect user input;
- Submit data to a server for processing;
- Build search boxes, login forms, registration pages, and similar interfaces;
- Develop interactive web applications.
This article walks you through the form tags behind these features. Whether you want a simple message board or a complete registration page, these are essential skills.
🧾 What Is a Form?
A form is an HTML structure for collecting user input.
The <form> tag wraps input controls, such as fields and buttons, and submits the data users enter to a specified destination.
Here is the basic structure:
<form action="submission-url" method="submission-method">
<!-- Input controls go here -->
</form>
action: The server URL that receives the data.method: The submission method, commonlygetorpost.
🖊 Input Fields (input)
Input fields are common form controls for single-line input, such as usernames and email addresses.
<form>Username: <input type="text" name="username" /></form>
Common input types include:
| Type | Description | Example |
|---|---|---|
| text | Text input | Usernames and display names |
| password | Password input | Masks entered characters, for example during login |
| Email input with validation | Checks whether the value has an email address format | |
| number | Numeric input | Accepts numeric values |
| date | Date input | Displays a date picker |
| checkbox | Checkbox | Allows multiple selections |
| radio | Radio button | Allows one selection from a group |
📑 Drop-Down Lists (select)
Use <select> when users should choose from a set of options. Define each option with <option>.
<form>
Gender:
<select name="gender">
<option value="male">Male</option>
<option value="female">Female</option>
<option value="other">Other</option>
</select>
</form>
valuespecifies the data submitted;- The text between the
<option>tags is what users see.
📝 Multiline Text Fields (textarea)
Use <textarea> for longer input, such as a message or biography.
<form>
Message:
<textarea name="message" rows="4" cols="30"></textarea>
</form>
rows: The number of visible text lines;cols: The visible width in characters;<textarea>requires both opening and closing tags. Do not use self-closing syntax.
🧷 Submit Buttons (submit)
Once users finish filling out a form, they need a way to submit it. Use <input type="submit"> or <button>.
<form action="/submit" method="post">
Name: <input type="text" name="name" />
<input type="submit" value="Submit" />
</form>
Alternatively, use the more flexible button element:
<form>
<button type="submit">Submit form</button>
</form>
Both approaches submit the form.
❓ Questions and Answers
Q1: Must every <input> be used with a <form>?
No. However, for a native form submission to collect and send its data to a backend, the input needs to belong to a <form>.
Q2: How can I submit a form without reloading the page?
Use JavaScript to intercept the submit event, then submit the data asynchronously with XMLHttpRequest or fetch.
Q3: Can a drop-down list have a default selection?
Yes. Add the selected attribute to the appropriate <option>.
🧪 Exercises
Exercise 1: Create a Registration Form
Use form tags to create a registration form with username, email, and password fields.
<form action="/register" method="post">
Username: <input type="text" name="username" /><br />
Email: <input type="email" name="email" /><br />
Password: <input type="password" name="password" /><br />
<input type="submit" value="Register" />
</form>
Copy this code into your editor and preview the page!
Exercise 2: Create a Message Form
Use <textarea> for the message field and add a submit button.
<form action="/message" method="post">
Message:<br />
<textarea name="message" rows="5" cols="40"></textarea><br />
<button type="submit">Send message</button>
</form>
Copy this code into your editor and preview the page!
🎉 You now know how to use forms, common input controls, and submission methods! Try building a complete registration page or comment system next!
