Bytes

HTML Form Tag - Action, Name, Target

Last Updated: 22nd June, 2023

Form attributes - action, name, target

Introduction

In HTML, forms are used to collect data from the user. They allow the user to input information, such as text, numbers, or files, and then send that information to a web server for processing. Forms are often used for a variety of purposes, including:

  • Contact forms
  • Search boxes
  • Login and registration forms
  • Feedback forms
  • Order and checkout forms

Forms are created using HTML tags, such as the <form> tag, which defines the start and end of a form, and the <input> tag, which is used to create form fields for the user to fill out. Form fields can include text boxes, dropdown lists, checkboxes, and more. The data collected from the form can be processed using server-side programming languages such as PHP, ASP, or Python.

Forms can also be styled using CSS, which allows developers to customize the appearance of form elements to match the design of their website.

The basic structure of an HTML form consists of the following elements:

  1. <form> tag: This is used to define the beginning and end of the form. It contains all the form elements that the user will interact with.
  2. Form fields: These are the individual elements that make up the form, such as text boxes, dropdown lists, checkboxes, and radio buttons. Form fields are created using the <input> tag, and each field has its own unique attributes.
  3. Submit button: This is a button that the user clicks on to submit the form data to the server for processing. It is created using the <input> tag with a type of "submit".

Here's an example of a basic HTML form structure:

<form>
  <label for="name">Name:</label>
  <input type="text" id="name" name="name"><br><br>

  <label for="email">Email:</label>
  <input type="email" id="email" name="email"><br><br>

  <label for="message">Message:</label>
  <textarea id="message" name="message"></textarea><br><br>

  <input type="submit" value="Submit">
</form>

In this example, the form contains three form fields: a text input for the user's name, an email input for their email address, and a textarea for their message. The for attribute in the <label> tag is used to associate the label with its corresponding form field, and the name attribute in the <input> and <textarea> tags is used to identify the form data when it is submitted. Finally, the form has a submit button that the user can click to submit their data to the server.

Now, we'll take a closer look at some of the most commonly used form attributes in HTML5, including the action attribute, method attribute, name attribute, placeholder attribute, required attribute, disabled attribute, autocomplete attribute, and readonly attribute.

Action Attribute

The action attribute is used to specify the URL of the server-side script that will handle the form data when the user submits the form. The value of the action attribute should be a valid URL, and can be either an absolute or a relative URL. If the action attribute is not specified, the form data will be submitted to the current URL.

Here's an example of the action attribute in use:

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

In this example, the action attribute specifies that the form data should be submitted to the "/submit-form.php" URL.

Method Attribute

The method attribute is used to specify the HTTP method that should be used to submit the form data. The two most commonly used methods are "GET" and "POST". When the GET method is used, the form data is appended to the URL as a query string, while with the POST method, the form data is sent in the request body.

Here's an example of the method attribute in use:

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

In this example, the method attribute specifies that the form data should be submitted using the POST method.

Name Attribute

The name attribute is used to identify a form element when the form is submitted. This attribute is used to associate the label with the form element and to access the form data on the server-side script. The name attribute can be used with most form elements, including input fields, select boxes, and text areas.

Here's an example of the name attribute in use:

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

In this example, the name attribute is used to identify the input field for the username.

Placeholder Attribute

The placeholder attribute is used to provide a hint or example text for the user when they are filling out a form field. This attribute is typically used with input fields and text areas.

Here's an example of the placeholder attribute in use:


<label for="email">Email:</label>
<input type="email" name="email" id="email" placeholder="Enter your email address">

In this example, the placeholder attribute provides an example of the type of data that the user should enter in the email field.

Required Attribute

The required attribute is used to indicate that a form field must be filled out before the form can be submitted. This attribute is typically used with input fields and select boxes.

Here's an example of the required attribute in use:

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

In this example, the required attribute indicates that the username field must be filled out before the form can be submitted.

Disabled Attribute

The disabled attribute is used to disable a form field so that it cannot be edited or submitted by the user. This attribute is typically used with input fields and select boxes.

Here's an example of the disabled attribute in use:

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

In this example, the disabled attribute disables the username field so that it cannot be edited by the user.

Autocomplete Attribute

The autocomplete attribute is used to control whether a form field should be autocompleted by the browser. This attribute is typically used with input fields and text areas.

Here's an example of the autocomplete attribute in use:

<label for="address">Address:</label>
<input type="text" name="address" id="address" autocomplete="on">

In this example, the autocomplete attribute is set to "on" to indicate that the browser should autocomplete the address field.

Readonly Attribute

The readonly attribute is used to make a form field read-only, which means that the user can view the value of the field, but cannot edit or change it. This attribute is typically used with input fields and text areas.

Here's an example of the readonly attribute in use:

<label for="username">Username:</label>
<input type="text" name="username" id="username" value="johndoe" readonly>

In this example, the readonly attribute makes the username field read-only, and sets its initial value to "johndoe".

Conclusion

In conclusion, HTML forms are a crucial component of web development as they allow users to input data that is processed by server-side programming languages. The use of form attributes such as the action attribute, method attribute, name attribute, placeholder attribute, required attribute, disabled attribute, autocomplete attribute, and readonly attribute can enhance the functionality and appearance of forms. By utilizing these attributes, web developers can ensure that their forms are both easy to use and visually appealing, ultimately resulting in a better user experience.

Module 6: HTML5 AttributesHTML Form Tag - Action, Name, Target

Top Tutorials

Related Articles

  • Official Address
  • 4th floor, 133/2, Janardhan Towers, Residency Road, Bengaluru, Karnataka, 560025
  • Communication Address
  • Follow Us
  • facebookinstagramlinkedintwitteryoutubetelegram

© 2024 AlmaBetter