HTML Forms

Collect user input with the form element.

✍️ 3 exercises + code challenge in this lesson

An HTML form collects user input, which is usually sent to a server for processing.

  • <form> wraps the form controls.
  • <label> describes a field; its for attribute matches the field's id.
  • <input> is the most common field; its type decides how it behaves.
  • The name attribute is the key used when the data is sent. A field without a name is not submitted.
Clicking a label focuses its field. Always use labels: they make forms easier to use and accessible.

Key Takeaways: HTML Forms

Mastering HTML Forms helps you write clean and reliable HTML code. Follow standard conventions, test edge cases, and keep logic simple. Try running the code example below and complete the exercises to build confidence.

HTML Forms Code Example

HTML
<form onsubmit="alert('Welcome, ' + this.fname.value + ' ' + this.lname.value + '!'); return false;">
  <label for="fname">First name:</label><br>
  <input type="text" id="fname" name="fname" value="Ali"><br><br>
  <label for="lname">Last name:</label><br>
  <input type="text" id="lname" name="lname" value="Khan"><br><br>
  <input type="submit" value="Submit">
</form>
Free sign-in required · Google login available

HTML Forms Practice Exercises

Check what you learned in “HTML Forms”.

/ 3 solved
Fill in the blank

Connect the label to the input.

<label ="email">Email</label>
<input id="email" name="email">
✓ Correct! Not quite. Try again.
Multiple choice

Which attribute must an input have to be sent with the form?

✓ Correct! Not quite. Try again.
Code challenge

Create a form with a text input named "username" and a submit button.

Write the code in the live editor. Your answer is checked automatically.

Free sign-in required · Google login available

to save your exercise progress.
Finished this lesson?
Mark it complete to track your progress.

Stuck on something?

Ask the community in the CodesInTech forum.

Need a developer?

Learning on your own is great — and when you need professional help, our team builds custom software, websites and mobile apps for clients worldwide.

Explore Our Services