HTML <form> Element
The <form> tag is used to create an HTML form for collecting user input. It can contain various input elements such as text fields, checkboxes, radio buttons, submit buttons, etc. When a form is submitted, the data entered by the user is sent to a server for processing.
Basic Syntax
<form action="url_to_submit_data" method="post/get">
</form>
The <form> tag has two main attributes:
| Attribute | Description |
action |
Specifies the URL to which the form data will be submitted. This URL can be a relative or absolute path. When the form is submitted, the browser will navigate to this URL. |
method |
Specifies how the form data will be sent. The two most common methods are post and get. post sends the data in the body of the request, while get appends the data to the URL. |
Example
Let's create a simple login form using the <form> tag:
<form action="/login" method="post">
<label for="username">Username:</label>
<input type="text" id="username" name="username">
<br>
<label for="password">Password:</label>
<input type="password" id="password" name="password">
<br>
<input type="submit" value="Login">
</form>
In this example, when the user enters their username and password and clicks the "Login" button, the form will be submitted to the "/login" URL using the post method.
Remember to always include a submit button (input with type="submit") in your form so that users can submit their input.