HTML Form

Let’s start from the very basics and go step-by-step to explain HTML Form in detail, from start to finish.

An HTML form is a section of a webpage that allows users to input data and submit it to a server for processing. Forms are used for various purposes, such as:

  • Logging into a website
  • Registering for an account
  • Searching for content
  • Submitting feedback or contact information

Every form in HTML is created using the <form> element. Here’s the basic structure:

<form action="/submit-url" method="post">
  <!-- Form elements go here -->
</form>
HTML

  • <form>: The container for all form elements.
  • action: Specifies the URL where the form data will be sent when submitted.
  • method: Defines the HTTP method used to send data:
  • get: Appends form data to the URL (visible in the address bar).
  • post: Sends data in the HTTP request body (not visible in the URL).

Forms contain various input elements to collect data. Here are the most common ones:

Used for single-line text input, like a username or email.

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

  • <label>: Describes the purpose of the input field.
  • <input>: Creates the input field.
  • type=”text”: Specifies a text input.
  • id: Links the input to the label.
  • name: Identifies the input when the form is submitted.

Used for sensitive data like passwords.

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

  • type=”password”: Masks the input with asterisks (*) or dots.

Used for multi-line text input, like comments or messages.

<label for="message">Message:</label>
<textarea id="message" name="message" rows="5" cols="30"></textarea>
HTML

  • <textarea>: Creates a multi-line text box.
  • rows and cols: Define the size of the textarea.

Used for options where multiple selections are allowed.

<label for="subscribe">Subscribe to newsletter:</label>
<input type="checkbox" id="subscribe" name="subscribe" value="yes">
HTML

  • type=”checkbox”: Allows multiple selections.
  • value: The data sent to the server if the checkbox is checked.

Used for options where only one selection is allowed.

<label for="gender-male">Male:</label>
<input type="radio" id="gender-male" name="gender" value="male">

<label for="gender-female">Female:</label>
<input type="radio" id="gender-female" name="gender" value="female">
HTML

  • type=”radio”: Allows only one selection from a group.
  • name: Groups radio buttons together (only one can be selected).

Used to select one option from a list.

<label for="country">Country:</label>
<select id="country" name="country">
  <option value="us">United States</option>
  <option value="ca">Canada</option>
  <option value="uk">United Kingdom</option>
</select>
HTML

  • <select>: Creates a dropdown menu.
  • <option>: Defines each item in the dropdown.

Used to upload files.

<label for="file-upload">Upload a file:</label>
<input type="file" id="file-upload" name="file">
HTML

  • type=”file”: Allows users to select a file from their device.

Used to submit the form.

<input type="submit" value="Submit">
HTML

  • type=”submit”: Creates a button to send the form data to the server.
  • value: The text displayed on the button.

Used to clear the form.

<input type="reset" value="Reset">
HTML

  • type=”reset”: Clears all form inputs to their default values.

Here’s a complete example of an HTML form:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Sample Form</title>
</head>
<body>
  <h1>Registration Form</h1>
  <form action="/submit" method="post">
    <!-- Text Input -->
    <label for="username">Username:</label>
    <input type="text" id="username" name="username" required><br><br>

    <!-- Password Input -->
    <label for="password">Password:</label>
    <input type="password" id="password" name="password" required><br><br>

    <!-- Radio Buttons -->
    <label>Gender:</label>
    <input type="radio" id="gender-male" name="gender" value="male"> Male
    <input type="radio" id="gender-female" name="gender" value="female"> Female<br><br>

    <!-- Dropdown Menu -->
    <label for="country">Country:</label>
    <select id="country" name="country">
      <option value="us">United States</option>
      <option value="ca">Canada</option>
      <option value="uk">United Kingdom</option>
    </select><br><br>

    <!-- Textarea -->
    <label for="message">Message:</label><br>
    <textarea id="message" name="message" rows="5" cols="30"></textarea><br><br>

    <!-- Submit Button -->
    <input type="submit" value="Register">
    <!-- Reset Button -->
    <input type="reset" value="Clear">
  </form>
</body>
</html>
HTML

required: Makes an input field mandatory.

   <input type="text" name="username" required>
HTML

placeholder: Displays a hint inside the input field.

   <input type="text" name="email" placeholder="Enter your email">
HTML

autocomplete: Enables or disables autocomplete.

   <input type="text" name="email" autocomplete="off">
HTML

disabled: Disables an input field.

   <input type="text" name="username" disabled>
HTML

You can style forms using CSS to make them visually appealing. For example:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Styled HTML Form</title>
  <style>
    /* CSS Styles */
    form {
      max-width: 400px;
      margin: 0 auto;
      padding: 20px;
      border: 1px solid #ccc;
      border-radius: 5px;
    }

    input[type="text"], input[type="password"], textarea, select {
      width: 100%;
      padding: 10px;
      margin: 5px 0;
      border: 1px solid #ccc;
      border-radius: 4px;
    }

    input[type="submit"] {
      background-color: #4CAF50;
      color: white;
      padding: 10px 20px;
      border: none;
      border-radius: 4px;
      cursor: pointer;
    }

    input[type="submit"]:hover {
      background-color: #45a049;
    }

    label {
      font-weight: bold;
      display: block;
      margin-top: 10px;
    }
  </style>
</head>
<body>
  <h1 style="text-align: center;">Registration Form</h1>
  <form action="/submit" method="post">
    <!-- Text Input -->
    <label for="username">Username:</label>
    <input type="text" id="username" name="username" required>

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

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

    <!-- Dropdown Menu -->
    <label for="country">Country:</label>
    <select id="country" name="country">
      <option value="us">India</option>
      <option value="ca">Canada</option>
      <option value="uk">United Kingdom</option>
    </select>

    <!-- Textarea -->
    <label for="message">Message:</label>
    <textarea id="message" name="message" rows="5" required></textarea>

    <!-- Submit Button -->
    <input type="submit" value="Register">
  </form>
</body>
</html>
HTML

output:

Registration Form

HTML forms are a fundamental part of web development, enabling user interaction and data collection. By combining HTML, CSS, and JavaScript, you can create dynamic and user-friendly forms for your websites. Practice building forms with different input types and attributes to master this essential skill!