DEV Community

Geoff
Geoff

Posted on

Introduction to HTML forms

HTML Forms: A Complete Beginner's Tutorial

HTML forms are an essential part of web development, enabling users to send data to a server. They are the foundation for user input, used in login pages, surveys, search bars, and more. In this tutorial, we’ll cover the basics of HTML forms, including structure, elements, and common attributes.

1. Structure of an HTML Form

At its core, an HTML form is created using the <form> element. This tag wraps all the form-related elements such as input fields, checkboxes, buttons, etc.

Here's the basic structure:

<form action="/submit-form" method="POST">
    <!-- Form elements go here -->
</form>
Enter fullscreen mode Exit fullscreen mode
  • action: Specifies where the form data should be sent. It is usually a URL to a server-side script (like PHP, Python, etc.).
  • method: Specifies the HTTP method to be used when sending form data. The two common values are:
    • GET: Sends data via the URL (good for searches or non-sensitive data).
    • POST: Sends data in the HTTP request body (preferred for sensitive or large amounts of data).

2. Form Elements

A form can contain a variety of elements, each allowing for different types of user input. Let’s look at the most common ones.

a) Text Input (<input>)

Text inputs allow users to type short strings.

<form action="/submit-form" method="POST">
    <label for="name">Name:</label>
    <input type="text" id="name" name="username" required>
</form>
Enter fullscreen mode Exit fullscreen mode
  • type="text": Defines a single-line text input.
  • id: Associates the input with a label for accessibility.
  • name: Identifies the form data when it’s sent to the server.
  • required: Ensures the user fills in the field before submission.

b) Password Input

For password fields, where you want the input to be hidden:

<label for="password">Password:</label>
<input type="password" id="password" name="password" required>
Enter fullscreen mode Exit fullscreen mode
  • type="password": Obscures the input text (useful for passwords).

c) Radio Buttons

Radio buttons let users choose one option from a list.

<label for="gender">Gender:</label>
<input type="radio" id="male" name="gender" value="male">
<label for="male">Male</label>
<input type="radio" id="female" name="gender" value="female">
<label for="female">Female</label>
Enter fullscreen mode Exit fullscreen mode
  • type="radio": Allows the user to select only one option from a group.
  • value: Represents the value sent to the server if this option is selected.

d) Checkboxes

Checkboxes allow users to select multiple options.

<label for="hobbies">Hobbies:</label>
<input type="checkbox" id="hobby1" name="hobby" value="reading">
<label for="hobby1">Reading</label>
<input type="checkbox" id="hobby2" name="hobby" value="sports">
<label for="hobby2">Sports</label>
Enter fullscreen mode Exit fullscreen mode
  • type="checkbox": Lets users select one or more options.

e) Drop-Down Menus (<select>)

Drop-down menus provide users with a list of options.

<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>
Enter fullscreen mode Exit fullscreen mode
  • <select>: Contains a list of options.
  • <option>: Defines each selectable option.

f) Text Area (<textarea>)

For larger text input, such as comments or messages, you can use a <textarea>.

<label for="message">Message:</label>
<textarea id="message" name="message" rows="4" cols="50"></textarea>
Enter fullscreen mode Exit fullscreen mode
  • <textarea>: Allows multi-line text input.

g) Submit Button

To send form data, you need a submit button:

<input type="submit" value="Submit">
Enter fullscreen mode Exit fullscreen mode
  • type="submit": When clicked, this button submits the form to the server.

3. Example Form

Here’s a complete form example that combines all the elements:

<form action="/submit-form" method="POST">
    <label for="username">Username:</label>
    <input type="text" id="username" name="username" required><br><br>

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

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

    <label for="hobbies">Hobbies:</label>
    <input type="checkbox" id="hobby1" name="hobby" value="reading">
    <label for="hobby1">Reading</label>
    <input type="checkbox" id="hobby2" name="hobby" value="sports">
    <label for="hobby2">Sports</label><br><br>

    <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>

    <label for="message">Message:</label>
    <textarea id="message" name="message" rows="4" cols="50"></textarea><br><br>

    <input type="submit" value="Submit">
</form>
Enter fullscreen mode Exit fullscreen mode

4. Form Validation

HTML5 introduced a number of input validation attributes. For example:

  • required: Ensures the field is filled.
  • pattern: Allows you to define a regular expression pattern for the input.
  • min, max, minlength, maxlength: Set numerical and character limits.

Example:

<input type="text" name="zip" pattern="\d{5}" title="Enter a 5-digit ZIP code" required>
Enter fullscreen mode Exit fullscreen mode

5. Styling Forms

You can style forms using CSS to improve their appearance. Here’s a simple example:

<style>
    form {
        max-width: 400px;
        margin: 0 auto;
    }
    label {
        display: block;
        margin-bottom: 8px;
    }
    input, select, textarea {
        width: 100%;
        padding: 8px;
        margin-bottom: 12px;
        border: 1px solid #ccc;
        border-radius: 4px;
    }
</style>
Enter fullscreen mode Exit fullscreen mode

6. Handling Form Data

Form data can be handled server-side using languages like PHP, Python, Node.js, etc. The data is sent based on the action URL and can be accessed via GET or POST methods.

Example in PHP:

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $username = $_POST['username'];
    $password = $_POST['password'];
    // Process the data
}
?>
Enter fullscreen mode Exit fullscreen mode

HTML forms are an essential part of web development, allowing users to interact with your website. By combining different form elements, using proper validation, and styling with CSS, you can create powerful and user-friendly forms.

Be sure to check out the form backend service fabform.io

Top comments (0)