Form Attributes

Forms are a cornerstone of interactive web applications, allowing users to input and submit data. In HTML, the <form> element is enriched with attributes that control its behavior, interaction, and styling. Let's explore some of the most commonly used form attributes!

1. action

The action attribute specifies the URL where the form data should be sent after submission.

HTMLcopy
<form action="submit.php">
  <!-- Form fields go here -->
</form>

Example: If your action is submit.php, the data will be sent to this file for processing.

2. Method

The method attribute defines how the form data is sent. It has two main options:

  • GET: Appends data to the URL (good for search forms).
  • POST: Sends data securely in the body (preferred for sensitive data).
HTMLcopy
<form action="/submit" method="POST">
  <!-- Form fields go here -->
</form>

3. enctype

Specifies how the form data is encoded when submitted. Common values:

  • application/x-www-form-urlencoded(default): For simple text-based data.
  • multipart/form-data: For file uploads.
  • text/plain: For plain text submission.
HTMLcopy
<form action="/upload" method="POST" enctype="multipart/form-data">
  <input type="file" name="file">
</form>

4. target

Determines where to display the response after the form is submitted.

  • _self(default): Current tab.
  • _blank: New tab/window.
  • _parent: Parent frame.
  • _top: Topmost frame.
HTMLcopy
<form action="/search" target="_blank">
  <input type="text" name="query">
</form>

5. autocomplete

Enables or disables auto-fill for form fields.

  • on(default): Browser fills known values.
  • off: Prevents auto-fill.
HTMLcopy
<form action="/login" autocomplete="off">
  <input type="text" name="username">
</form>

6. novalidate

Prevents the browser from automatically validating the form inputs.

HTMLcopy
<form action="/submit" novalidate>
  <input type="email" name="email">
</form>

7. name

Assigns a name to the form for easier identification, especially when using JavaScript.

HTMLcopy
<form name="registrationForm">
  <!-- Form fields go here -->
</form>

8. rel

Provides additional information about the form, often used for security-related contexts like preventing CSRF (Cross-Site Request Forgery) in modern web apps.

HTMLcopy
<form name="registrationForm">
  <!-- Form fields go here -->
</form>



HTML form attributes make forms flexible, secure, and efficient. By combining them effectively, you can build powerful forms tailored to user needs.

Page 31 of 33

Page 31 of 33

logoFALYTOM

Learn technologies like HTML, CSS, JavaScript, React, NodeJS and so much more. Practice your skills with our free games, examples, practice questions & quizzes

    © 2024 Tomilola Group
    • facebook logo
    • twitter logo
    • instagram logo
    • linkedin logo
    • github logo
    • youtube logo