Back to blog

September 06, 2024

avataravatar

Gautam Patoliya, Deep Poradiya

Tutor Head

Understanding HTML Form Attributes

blog-img-Understanding HTML Form Attributes

Understanding HTML Form Attributes

HTML forms have several key attributes that control how form data is sent, processed, and displayed. These attributes allow developers to dictate where data goes, how it’s transmitted, and how the response is displayed. In this blog, we’ll explore the three most commonly used form attributes: action, method, and target.


1. action Attribute


What it does:

The action attribute specifies the URL where the form data will be sent once the form is submitted. This URL can point to a server-side script (like PHP) or an API endpoint that handles the data.


How to use it:

Set the action attribute to the URL where you want the form data to go.


<form action="https://toforzero.com/submit-form">
  <label for="username">Username:</label>
  <input type="text" id="username" name="username">
  <button type="submit">Submit</button>
</form>


When the user submits this form, the data will be sent to https://toforzero.com/submit-form.


Common Use Cases:

  • Sending data to a specific page: Use the action attribute to point to a server-side script or page that processes the form data.
  • Sending data to an API: In modern web apps, the action often points to an API endpoint.


2. method Attribute


What it does:

The method attribute defines how the form data is transmitted to the server. The two most common methods are GET and POST.


How to use it:

Set the method attribute to either get or post.


Example:

<form action="https://example.com/submit-form" method="post">
  <label for="email">Email:</label>
  <input type="email" id="email" name="email">
  <button type="submit">Submit</button>
</form>


This form uses the POST method to send data securely to the server.


GET vs POST:


  • GET Method:
  • Sends data as query parameters in the URL (e.g., https://toforzero.com/submit-form?email=user@toforzero.com).
  • Less secure because data is visible in the URL, making it unsuitable for sensitive information (like passwords).
  • Used for: Retrieving data, such as search queries.


  • POST Method:
  • Sends data in the body of the HTTP request, not in the URL.
  • More secure since data isn't visible in the URL, making it ideal for sensitive data.
  • Used for: Submitting forms that alter server data, such as user registrations or file uploads.


3. target Attribute


What it does:

The target attribute controls where the response to a submitted form is displayed. It determines whether the response opens in the same tab, a new tab, a frame, or a popup window.


How to use it:

Set the target attribute to one of the following values:

  • _self: (Default) Opens the response in the same window or tab.
  • _blank: Opens the response in a new tab or window.
  • _parent: Opens the response in the parent frame (if using frames).
  • _top: Breaks out of any frames and opens the response in the full window.


Example:

<form action="https://example.com/submit-form" method="post" target="_blank">
  <label for="name">Name:</label>
  <input type="text" id="name" name="name">
  <button type="submit">Submit</button>
</form>


When this form is submitted, the response will open in a new tab or window.

HTML