Master Code
On The Go

Learn. Practice. Build.

Home › Code Snippets › HTML Snippets › Reference › <form>

HTML Reference

<form> — The Form Element

The container that turns static HTML into an interactive page. Collects user input and sends it to a server.

Last updated: October 2026

Definition: The <form> element wraps interactive controls (inputs, buttons, selects) and defines how their data is packaged and sent to a server. It has three essential attributes: action (where to send), method (how to send), and enctype (encoding for file uploads).

🎬 Live Example

A working form. Press Enter or click Submit — the browser validates before sending.

💡 The required attribute on the inputs triggers native browser validation before the form submits.

📝

Basic Syntax

<form action="/submit" method="POST">
    <label for="email">Email</label>
    <input type="email" id="email" name="email" required>

    <button type="submit">Submit</button>
</form>

The form itself has no visible appearance. It is a container that groups inputs and defines submission behavior.

⚙️ Attributes Reference

Attribute Value Purpose
actionURLWhere to send form data on submit.
methodGET / POSTHow to send the data. Default is GET.
enctypeapplication/x-www-form-urlencoded (default), multipart/form-data, text/plainHow to encode data. Required for file uploads.
autocompleteon / offWhether the browser can autofill fields.
novalidatebooleanDisable browser validation.
target_self, _blank, etc.Where to display the response.
accept-charsetcharsetCharacter encoding for the form data.
nametextForm name, used in JavaScript.
1

GET vs POST — Choosing the Right Method

Feature GET POST
Data locationURL query stringRequest body
Visible in URLYesNo
BookmarkableYesNo
Size limit~2000 charsUnlimited
Use forSearch, filtersLogins, signups, data changes
Secure for passwordsNoYes

Rule: Never use GET for anything sensitive (passwords, tokens) or for anything that modifies data on the server. Use POST for both.

2

File Upload Form

File uploads require method="post" and enctype="multipart/form-data".

<form action="/upload" method="POST" enctype="multipart/form-data">
    <label for="avatar">Profile Picture</label>
    <input type="file" id="avatar" name="avatar" accept="image/*">

    <button type="submit">Upload</button>
</form>

Without enctype="multipart/form-data", the file contents won't be sent — only the filename.

3

Accessible Form Structure

Group related fields with <fieldset> and <legend>.

<form action="/signup" method="POST">
    <fieldset>
        <legend>Personal Information</legend>

        <label for="name">Full Name</label>
        <input type="text" id="name" name="name" required>

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

    <fieldset>
        <legend>Account Details</legend>

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

        <label for="password">Password</label>
        <input type="password" id="password" name="password" required minlength="8">
    </fieldset>

    <button type="submit">Create Account</button>
</form>
4

Prevent Submission with JavaScript

Common pattern — validate or submit via AJAX.

<form id="signup-form" action="/signup" method="POST">
    <input type="email" name="email" required>
    <button type="submit">Sign Up</button>
</form>

<script>
    const form = document.getElementById("signup-form");

    form.addEventListener("submit", async (e) => {
        e.preventDefault();  // Stop the browser's default submit

        const data = new FormData(form);

        const response = await fetch("/signup", {
            method: "POST",
            body: data
        });

        if (response.ok) {
            showSuccess();
        } else {
            showError();
        }
    });
</script>
5

Autocomplete Best Practices

Give browsers the right hints so they can autofill correctly.

<form action="/checkout" method="POST">
    <input type="text" name="name" autocomplete="name">
    <input type="email" name="email" autocomplete="email">
    <input type="tel" name="phone" autocomplete="tel">
    <input type="text" name="street" autocomplete="street-address">
    <input type="text" name="city" autocomplete="address-level2">
    <input type="text" name="zip" autocomplete="postal-code">
    <input type="text" name="cc-number" autocomplete="cc-number">
    <input type="text" name="cc-exp" autocomplete="cc-exp">
    <input type="password" name="new-password" autocomplete="new-password">
</form>

Browser autofill reduces typing errors and speeds up checkout by up to 30%. Always use the correct autocomplete values.

🎯 enctype Values

Value Use for
application/x-www-form-urlencodedNormal text forms (default).
multipart/form-dataForms with file uploads. Required.
text/plainDebugging only. Rarely used in production.

⚠️ Common Mistakes

♿ Accessibility Notes

❓ Frequently Asked Questions

What is the HTML <form> tag?

The <form> element is a container for interactive controls (inputs, buttons, checkboxes) that collect user data and submit it to a server. It defines how and where the data is sent.

What is the difference between GET and POST?

GET sends form data as URL query parameters — visible in the address bar, limited in size, and not secure for sensitive data. POST sends data in the request body — not visible in the URL, no size limit, and the standard choice for logins, signups, and any data that modifies the server.

What is enctype in a form?

enctype sets the encoding type for form data when using method="post". Use application/x-www-form-urlencoded (default) for normal forms, multipart/form-data for file uploads, and text/plain rarely.

Do I need a form for every input?

If the inputs collect data to be submitted together, yes. Wrap them in a single <form>. Inputs outside a form are only useful for JavaScript-driven interfaces where the data never leaves the page.

How do I prevent a form from submitting?

Use JavaScript: form.addEventListener('submit', e => e.preventDefault()). Use this when you're handling the submission with fetch() and need to keep the page from reloading.

🎯 What's Next?

← All HTML Reference