Skip to main content

HTML Tags

HTML <form> Tag

Written by Updated HTML <form> Tag exercises

The form tag wraps a set of fields and sends them somewhere when the user presses Submit. Its attributes decide where (action), how (method) and in what format (enctype). Most form bugs are a missing one of these three.

This is the tag reference. Every attribute is explained with its own example in HTML Form Attributes, and how forms work as a whole in HTML form basics.

Syntax

html

<form action="/samples/form_processor.php" method="post" target="_blank" enctype="multipart/form-data" autocomplete="on">
   <!-- form elements go here -->
</form>
AttributeWhat it does
actionThe address that receives the data. If missing, the form sends to the current page.
methodget adds the data to the address; post sends it hidden. The default is get.
enctypeHow the data is packed. File uploads need multipart/form-data.
targetWhere the response opens, like _blank for a new tab.
autocompleteTurns the browser's saved suggestions on or off.
novalidateSwitches off the browser's built-in checks.
nameA name, so scripts can find the form.

Example

html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Form Example</title>
</head>
<body>
  <h2>Registration Form</h2>
  <form action="/samples/form_processor.php" method="post" enctype="multipart/form-data">
    <label for="name">Full Name:</label><br>
    <input type="text" id="name" name="fullname" placeholder="Enter your name" required><br><br>

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

    <label for="file">Upload Photo:</label><br>
    <input type="file" id="file" name="photo"><br><br>

    <input type="submit" value="Register">
  </form>
</body>
</html>

The registration form has fields for a name, an email and a photo. Because there is a file field, the form uses method="post" and enctype="multipart/form-data"; with the default encoding, the server would receive only the file name.

Browser Support

Chrome
Edge
Firefox
Safari
Opera
IE9+
YesYesYesYesYesYes

Good to know

  • Forms cannot be nested. A <form> inside another <form> is invalid.
  • A field can belong to a form even when it sits outside it, with form="form-id".
  • Browser checks are for the user's convenience. Always check the data again on the server.

HTML <form> Tag exercises

Add a form

Make a form that sends to /subscribe. It has an email field named email and a button that sends it, with the text Join.

Result

Check your answer to see it here.