<form> HTML element is used to create a form in a web page. It acts as a container for various form elements such as input fields, checkboxes, radio buttons, submit buttons, and more. The form allows users to input and submit data, which can then be processed on the server-side or handled through client-side scripting.
Here’s an example of how to use the
<form action="/submit" 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" required></textarea> <button type="submit">Submit</button> </form>
In this example, the
<form> element wraps several form elements, including input fields and a textarea. The
action attribute specifies the URL where the form data should be sent when submitted, and the
method attribute specifies the HTTP method to use (typically “POST” or “GET”).
Each form element has a corresponding label associated with it using the
for attribute, which helps improve accessibility by linking the label to the form element. The
name attribute on each form element is used to identify the data on the server-side when the form is submitted.
<button> element with
type="submit" serves as the submit button for the form. When clicked, it triggers the submission of the form data to the server specified in the
pattern, to provide validation or specify specific input requirements.
It’s important to validate the form input on the server-side to ensure the submitted data is accurate and secure.
In summary, the
<form> element is used to create a form in HTML, allowing users to input and submit data. It serves as a container for form elements and facilitates data collection. The form data can be processed on the server-side or handled through client-side scripting for further interaction and validation.