HTML Tutorial

HTML Tag: <form>

Usage of the <form> Tag

The <form> tag in HTML is used to create a form that collects user input and submits it to a server for processing. It serves as a container for various input elements, such as text fields, radio buttons, and checkboxes.

Attributes of the <form> Tag

  • action: Specifies the URL where the form data will be submitted to.
  • method: Defines the HTTP method used to submit the form data, commonly either "GET" or "POST".
  • target: Indicates the window or frame in which the response from the server will be displayed.
  • enctype: Used to specify the encoding type used to send form data to the server.
  • name: Assigns a unique name to the form, which can be useful for identifying it later in scripts.

Examples of the <form> Tag

<!-- Form to collect user information and submit it to the "/process" URL -->
<form action="/process" method="POST">
  <label for="name">Name:</label>
  <input type="text" id="name" name="name" required>

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

  <input type="submit" value="Submit">
</form>

Exploring the <form> Tag

To explore the <form> tag in action, you can create a simple HTML file with the following code:

<!DOCTYPE html>
<html>
<head>
  <title>Example Form</title>
</head>
<body>
  <form action="/process" method="GET">
    <label for="name">Name:</label>
    <input type="text" id="name" name="name">

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

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

Save this file as "form.html" and open it in a web browser. You should see a simple form with two input fields and a submit button. When you enter your name and email and click the submit button, the form data will be sent to the "/process" URL using the GET method.