HTML Forms

A form collects input from the user, such as a search, a login, or a message.

The <form> element wraps the fields, and its action attribute says where the data should be sent when the form is submitted.

A Basic Form

Name field with a submit button

<!DOCTYPE html>
<html>
<body>

<form action="/submit">
  <label>Name:</label>
  <input type="text" name="name">
  <button type="submit">Send</button>
</form>

</body>
</html>

Key Parts of a Form

  • <form> wraps all the fields and controls where data is sent.
  • <label> gives each field a readable caption.
  • <input> collects a single value from the user.
  • <button type="submit"> sends the form.

Labels Linked to Inputs

for and id

<!DOCTYPE html>
<html>
<body>

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

</body>
</html>
Note: Give every input a name attribute. That name is the label attached to the value when the form is submitted.

Exercise: HTML Forms

What is the primary purpose of the <form> element?