Lesson 5: Forms & User Input

HYPERLINK CODE ACADEMY • HTML COURSE

Lesson 05: Forms & User Input

Learn how to collect information from people visiting your website.

01 — What Are HTML Forms?

Have you ever signed up for a website, entered your username, or sent someone a message through a contact page?

Those features often use HTML forms!

A form allows visitors to enter information and submit it. Forms are commonly used for:

  • Sign-up pages
  • Login screens
  • Contact forms
  • Search bars
  • Surveys and feedback

The main HTML tag for creating a form is <form>.

<form>
  <!-- Form elements go here -->
</form>

02 — The Input Tag

The <input> tag lets visitors enter information. Its type attribute determines what kind of input it creates.

Text input

<input type="text" placeholder="Enter your username">

Password input

<input type="password" placeholder="Enter your password">

Email input

<input type="email" placeholder="Enter your email">

Number input

<input type="number" placeholder="Enter your age">
Important: The placeholder attribute displays a hint inside the input. It is not the same as a label.

03 — Labels

Labels tell visitors what information belongs in an input field.

Use the <label> tag and connect it to an input using matching for and id values.

<label for="username">Username:</label>

<input
  type="text"
  id="username"
  name="username"
>

Why does this matter?

Labels make forms easier to understand and improve accessibility. Clicking a label can also focus its connected input.

04 — Buttons & Submitting Forms

Once visitors enter their information, they need a way to submit it.

Use a button with type="submit" inside your form.

<form>
  <label for="name">Your name:</label>
  <input type="text" id="name" name="name">

  <button type="submit">Submit</button>
</form>
Remember: HTML creates the form, but it does not automatically save information to a database. You need JavaScript and/or a backend service to process and store submissions.

05 — More Form Elements

Forms can do more than collect text. Here are some other useful elements.

Checkboxes

Checkboxes let visitors select one or more options.

<input type="checkbox" id="music" name="interests" value="music">
<label for="music">Music</label>

Radio buttons

Radio buttons let visitors choose one option from a group. Give the buttons the same name to group them.

<input type="radio" id="beginner" name="level" value="beginner">
<label for="beginner">Beginner</label>

<input type="radio" id="advanced" name="level" value="advanced">
<label for="advanced">Advanced</label>

Dropdown menus

Use <select> to create a dropdown and <option> for each choice.

<select name="favorite-color">
  <option value="blue">Blue</option>
  <option value="purple">Purple</option>
  <option value="pink">Pink</option>
</select>

Text areas

Use <textarea> when visitors need to write longer messages.

<textarea name="message" rows="4"></textarea>

06 — Build a Mini Sign-Up Form

Let's put everything together! Here's a simple sign-up form using several elements you've learned.

<form>
  <h2>Create an Account</h2>

  <label for="username">Username:</label>
  <input type="text" id="username" name="username" required>

  <label for="email">Email:</label>
  <input type="email" id="email" name="email" required>

  <label for="password">Password:</label>
  <input type="password" id="password" name="password" required>

  <button type="submit">Sign Up</button>
</form>

Try the example below!

Safety note: This is only a demonstration. Never enter a real password or private information into a practice form.

07 — Try It Yourself!

Your challenge is to create a simple contact form.

It should include:

  1. A heading that says "Contact Us".
  2. A name input.
  3. An email input.
  4. A dropdown menu with three different subjects.
  5. A text area for a message.
  6. A submit button.

Start with this code:

<!DOCTYPE html>
<html>
<head>
  <title>Contact Us</title>
</head>

<body>

  <h1>Contact Us</h1>

  <form>
    <!-- Build your form here -->
  </form>

</body>
</html>

See if you can build the entire form without copying the earlier example!

08 — Quick Knowledge Check

Question 1: Which HTML tag creates a form?


Question 2: Which input type hides the characters being typed?


Question 3: Which tag creates a dropdown menu?


Question 4: Does HTML automatically save form submissions to a database?

⚙