The form
tag is an HTML element that allows you to create a form on your web page. Forms are used to collect input from users, such as text, numbers, dates, and options. The form
tag provides a way to organize and structure the input fields, as well as to specify how the data should be processed and submitted.
Here’s an example of how to use the form
tag:
<form action="/submit-form" method="post"> <label for="name">Name:</label> <input type="text" id="name" name="name" required> <label for="email">Email:</label> <input type="email" id="email" name="email" required> <label for="message">Message:</label> <textarea id="message" name="message" rows="5" required></textarea> <button type="submit">Submit</button> </form>
In this example, we’re creating a simple form with three input fields: name, email, and message. The action
attribute specifies the URL of the script that will process the form data. The method
attribute specifies the HTTP method that will be used to submit the data, which can be either GET
or POST
.
Each input field is represented by an HTML element inside the form
tag. The label
element is used to associate the input field with its label. The for
attribute of the label
element should match the id
attribute of the input field.
The input
element is used to create text input fields, such as text, email, password, number, date, and checkbox. The type
attribute specifies the type of input field. The id
attribute is used to uniquely identify the input field, and the name
attribute specifies the name of the input field, which will be used to retrieve the data on the server.
The textarea
element is used to create a multiline text input field. The rows
attribute specifies the number of rows that should be visible.
The button
element is used to create a submit button. When the user clicks on the button, the form data will be submitted to the URL specified in the action
attribute, using the HTTP method specified in the method
attribute.
You can also add additional attributes to the form
tag, such as enctype
, autocomplete
, target
, and novalidate
. These attributes allow you to specify how the form data should be encoded, whether the browser should remember the input values, where the form should be submitted, and whether the browser should perform validation on the input fields before submitting the form.