Forms and User Input

Learn HTML forms and common input controls in this guide for beginning frontend developers.

🎯 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, commonly get or post.

🖊 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:

TypeDescriptionExample
textText inputUsernames and display names
passwordPassword inputMasks entered characters, for example during login
emailEmail input with validationChecks whether the value has an email address format
numberNumeric inputAccepts numeric values
dateDate inputDisplays a date picker
checkboxCheckboxAllows multiple selections
radioRadio buttonAllows one selection from a group

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>
  • value specifies 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!