Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
238 changes: 80 additions & 158 deletions docs/html/forms/building-form.mdx

Large diffs are not rendered by default.

254 changes: 100 additions & 154 deletions docs/html/forms/form-attribute.mdx
Original file line number Diff line number Diff line change
@@ -1,209 +1,155 @@
---
id: form-attribute
title: Form Attribute
sidebar_label: Form Attribute
sidebar_position: 3
description: "Learn about the form attribute in HTML forms and how it can be used to associate form elements with a form."
keywords: [form attribute, form element, form association, form control, HTML form, web development, front-end development, web design]
tags: [html, web-development, forms, user-input, front-end-development, web-design]
title: HTML Form Attributes
sidebar_label: Form Attributes
sidebar_position: 1
tags: [html form, html attributes, form action, form method, form target, form enctype]
description: "Learn about the essential attributes of the HTML <form> element, including action, method, target, and enctype, which control how form data is submitted and processed."
keywords: [html form attributes, form action, form method, form target, form enctype, form novalidate, html forms tutorial, web development]
---

The `form` attribute in HTML is used to associate form elements with a form. It specifies the `id` of the form element that the input element belongs to. This association allows you to group related form controls together and define the form structure more clearly.
The **`<form>` element** is the container for all your form fields (like `<input>`, `<textarea>`, and `<select>`). While the input fields collect the data, the `<form>` element itself defines **how** and **where** that data is sent once the user hits the submit button.

<AdsComponent />
<br />

Here's an example of how the `form` attribute can be used to associate form elements with a form:

```html title="index.html" showLineNumbers
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Form Attribute Example</title>
</head>
<body>
<h1>Form Attribute Example</h1>
<form id="user-form">
<label for="name">Name:</label>
<input type="text" id="name" name="name" form="user-form">
<br><br>
<label for="email">Email:</label>
<input type="email" id="email" name="email" form="user-form">
<br><br>
<button type="submit">Submit</button>
</form>
</body>
</html>
```

In the example, we have a form with the `id` attribute set to `"user-form"`. The `form` attribute of the `input` elements specifies that they belong to the form with the `id` `"user-form"`. This association allows the browser to group the form controls together and associate them with the form.
It achieves this control using specific **attributes** that tell the browser what to do with the collected information. Understanding these attributes is key to successful form handling.

<AdsComponent />
<br />

## Common Form Attributes
## Core Submission Attributes: Where and How

Here are some common attributes that can be used with the `form` attribute:
The two most critical attributes for any form are `action` and `method`. They dictate the destination and the transport mechanism for your data.

### 1. `action`
### 1. The `action` Attribute

The `action` attribute specifies the URL where the form data should be submitted when the form is submitted. It is used in conjunction with the `method` attribute to define the form submission behavior.
The `action` attribute specifies the **URL** where the form data should be sent (submitted) for processing when the form is submitted.

For example:
* **Value:** A valid URL (absolute or relative).
* **Purpose:** This URL points to the script or endpoint on a server (e.g., PHP, Python, Node.js) that will handle the incoming data.

```html title="index.html" showLineNumbers
<form id="user-form" action="/submit-form" method="post">
<!-- Form elements -->
```html
<form action="/scripts/process-signup.php" method="post">
</form>
```

### 2. `method`
### 2. The `method` Attribute

The `method` attribute specifies the HTTP method to be used when submitting the form data. It can be either `GET` or `POST`.
The `method` attribute defines the **HTTP method** used to send the form data. The two most common methods are `GET` and `POST`.

- `GET`: Appends data to the URL.
- `POST`: Sends data as part of the HTTP request body.
| Method | Description | When to Use |
| :--- | :--- | :--- |
| **`GET`** | Appends the form data to the URL as a query string (e.g., `url?name=value`). | Use for **non-sensitive data** and simple queries (like search forms) where users might want to bookmark the results. Data is visible in the URL. |
| **`POST`** | Packages the form data within the body of the HTTP request. | Use for **sensitive data** (passwords, credit cards) or when sending large amounts of data (like file uploads). Data is hidden from the URL. |

For example:

```html title="index.html" showLineNumbers
<form id="user-form" action="/submit-form" method="post">
<!-- Form elements -->
```html
<form action="/api/register" method="post">
</form>
```

### 3. `enctype`
---

## Behavior Attributes: Display and Encoding

These attributes control where the response from the server appears and how the data is formatted before transmission.

The `enctype` attribute specifies the encoding type of the form data when it is submitted to the server. It is used when the form contains file uploads.
### 3. The `target` Attribute

- `application/x-www-form-urlencoded`: Default for `POST` requests.
- `multipart/form-data`: Required for file uploads.
- `text/plain`: Sends data as plain text.
The `target` attribute specifies where to display the response received after submitting the form. Its function is identical to the `target` attribute used on the `<a>` (anchor) tag.

For example:
| Value | Description |
| :--- | :--- |
| **`_self`** | (Default) Loads the response in the same window/tab. |
| **`_blank`** | Loads the response in a new window or tab. |
| **`_parent`** | Loads the response in the parent frame (if using frames). |
| **`_top`** | Loads the response in the full body of the window. |

```html title="index.html" showLineNumbers
<form id="user-form" action="/submit-form" method="post" enctype="multipart/form-data">
<!-- Form elements -->
```html
<form action="/report" method="get" target="_blank">
</form>
```

### 4. `target`
### 4. The `enctype` Attribute (Encoding Type)

The `target` attribute specifies where to display the response that is received after submitting the form. It can be set to `_self`, `_blank`, `_parent`, or `_top`.
The `enctype` (Encoding Type) attribute specifies how the form data should be encoded when sending it to the server. This attribute is only relevant when the `method` is set to `post`.

- `_self`: Default, opens in the same window.
- `_blank`: Opens in a new tab or window.
- `_parent`: Opens in the parent frame.
- `_top`: Opens in the full body of the window.
| Value | Description | When to Use |
| :--- | :--- | :--- |
| **`application/x-www-form-urlencoded`** | (Default) All characters are encoded before sending. | Standard for almost all text-only forms. |
| **`multipart/form-data`** | No characters are encoded. | **MUST** be used if the user is uploading files (e.g., using `<input type="file">`). |
| **`text/plain`** | Spaces are converted to `+`, but no other special encoding. | Rarely used; mostly for debugging purposes. |

For example:

```html title="index.html" showLineNumbers
<form id="user-form" action="/submit-form" method="post" target="_blank">
<!-- Form elements -->
```html
<form action="/upload" method="post" enctype="multipart/form-data">
<input type="file" name="document">
</form>
```

<AdsComponent />
<br />
---

### 5. `autocomplete`
## Validation and Browser Control Attributes

The `autocomplete` attribute specifies whether the browser should automatically complete form fields based on the user's input history. It can be set to `on` or `off`.
These attributes give you control over browser-native features like auto-filling and form validation.

- `on`: Enables autofill (default).
- `off`: Disables autofill.
### 5. The `autocomplete` Attribute

For example:
The `autocomplete` attribute controls whether the browser should automatically fill in form values based on past user input.

```html title="index.html" showLineNumbers
<form id="user-form" action="/submit-form" method="post" autocomplete="off">
<!-- Form elements -->
* **Value:** `on` (Default) or `off`.

```html
<form autocomplete="off">
</form>
```

### 6. `novalidate`

The `novalidate` attribute disables the browser's built-in form validation when set to `novalidate`. This can be useful when custom validation logic is implemented using JavaScript.
:::note

For example:
`autocomplete` can also be set on individual `<input>` fields to control specific fields within a form.

```html title="index.html" showLineNumbers
<form action="/submit-data" method="post" novalidate>
<label for="phone">Phone:</label>
<input type="tel" id="phone" name="phone">

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

### 7. `name`
### 6. The `novalidate` Attribute

The `name` attribute assigns a unique identifier to the form. This is useful for backend processing.
By default, the browser performs **client-side validation** (e.g., checking for the `required` attribute or an email format) before submitting the form. The `novalidate` attribute overrides this behavior.

```html title="index.html" showLineNumbers
<form action="/process" method="post" name="registrationForm">
<label for="password">Password:</label>
<input type="password" id="password" name="password">
* **Value:** It's a **boolean attribute**, meaning its presence alone sets it to true.

<button type="submit">Submit</button>
```html
<form action="/submit" method="post" novalidate>
</form>
```

### 8. `accept-charset`

The `accept-charset` attribute specifies the character encodings supported by the server.

```html title="index.html" showLineNumbers
<form action="/submit" method="post" accept-charset="UTF-8">
<label for="feedback">Feedback:</label>
<textarea id="feedback" name="feedback"></textarea>

<button type="submit">Submit</button>
## Summary of Essential Form Attributes

| Attribute | Default Value | Purpose | When to Use |
| :--- | :--- | :--- | :--- |
| **`action`** | Current page URL | Specifies the **URL** where the form data is sent. | Always required for server submission. |
| **`method`** | `GET` | Specifies the **HTTP method** (`GET` or `POST`). | Use `POST` for sensitive data. |
| **`target`** | `_self` | Specifies **where** to display the server's response. | To open a response in a new tab (`_blank`). |
| **`enctype`** | `application/x-www-form-urlencoded` | Specifies how the data is encoded. | Must be `multipart/form-data` for file uploads. |
| **`autocomplete`** | `on` | Controls browser auto-filling suggestions. | To disable suggestions for sensitive forms (`off`). |
| **`novalidate`** | (Absent) | Tells the browser to skip client-side validation. | When server-side validation is preferred or necessary. |

## Example: A Complete Form Setup

Here is how you combine multiple attributes for a secure and functional user profile update form that includes a photo upload:

```html title="profile-update.html" showLineNumbers
<form action="/api/profile-update"
method="post"
enctype="multipart/form-data"
target="_self"
autocomplete="off">

<h2>Update Profile</h2>

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

<label for="photo">Profile Photo:</label>
<input type="file" id="photo" name="profile_photo">

<button type="submit">Save Changes</button>
</form>
```

By using the `form` attribute and other form attributes, you can create interactive and functional forms in HTML that collect user input and submit it to the server for processing.

<AdsComponent />
<br />

## Summary Table of Attributes

| Attribute | Description | Example Value |
|-----------------|------------------------------------------------------------|---------------------------|
| `action` | URL to submit form data to | `/submit-data` |
| `method` | HTTP method (`GET`, `POST`) | `post` |
| `target` | Specifies where to display the response | `_blank`, `_self` |
| `autocomplete` | Enables/disables browser autofill | `on`, `off` |
| `novalidate` | Disables browser validation | `novalidate` (boolean) |
| `enctype` | Encoding type for form data | `multipart/form-data` |
| `name` | Unique identifier for the form | `registrationForm` |
| `accept-charset`| Character encodings supported | `UTF-8` |

## Why Use the `form` Attribute?

Using the `form` attribute provides several benefits:

- **Improved Organization**: It helps in organizing form elements, especially when dealing with complex forms.
- **Better Accessibility**: Associating form controls with their respective forms enhances accessibility for users relying on assistive technologies.
- **Enhanced User Experience**: Properly structured forms lead to a better user experience, making it easier for users to fill out and submit forms.
- **Flexibility**: The `form` attribute allows form controls to be placed outside the actual `<form>` element, providing more flexibility in layout design.
- **Separation of Concerns**: It allows developers to separate form controls from the form structure, making it easier to manage and maintain code.
- **Consistent Behavior**: Ensures that all associated form controls are submitted together, maintaining data integrity.

<AdsComponent />
<br />

By understanding and utilizing the `form` attribute along with other form attributes, you can create well-structured, accessible, and user-friendly forms in HTML.

## Additional Resources
## Conclusion

- [MDN Web Docs: `<form>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/form)
- [W3Schools: HTML Forms](https://www.w3schools.com/html/html_forms.asp)
- [HTML Living Standard: Forms](https://html.spec.whatwg.org/multipage/forms.html)
- [CSS-Tricks: A Complete Guide to Forms](https://css-tricks.com/complete-guide-to-forms/)
- [WebAIM: Creating Accessible Forms](https://webaim.org/techniques/forms/)
The attributes of the `<form>` element are the controls that determine the communication pathway between the user's browser and your web server. By correctly setting the `action` (destination), `method` (transport type), and `enctype` (data format), you ensure that form data is submitted securely and correctly every time.
Loading