HTML, forms are used to collect user input. The <form> element is the container for all form elements, including input fields, buttons, checkboxes, radio buttons, and more.
Here's a basic example of an HTML form:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>HTML Form Example</title>
</head>
<body>
<h2>Contact Us</h2>
<form action="/submit_form" method="post">
<!-- Input fields and other form elements go here -->
<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="4" required></textarea>
<button type="submit">Submit</button>
</form>
</body>
</html>
In this example:
- The <form> element contains various form elements.
- Each form element is enclosed within the opening and closing tags of the <form> element.
- The action attribute specifies the URL to which the form data is submitted.
- The method attribute specifies the HTTP method (usually "get" or "post") used when submitting the form.
Common form elements include:
1. Text Input (<input type="text">):
Used for single-line text input.
2. Email Input (<input type="email">):
Used for email addresses. It includes basic email validation.
3. Textarea (<textarea>):
Used for multi-line text input, such as comments or messages.
4. Submit Button (<button type="submit">):
Submits the form data to the server.
5. Labels (<label>):
Provides a label for form elements. The for attribute should match the id attribute of the associated form element.
6. Required Attribute:
Indicates that the field must be filled out before submitting the form.
This is a basic example, and you can customize and extend it based on your specific requirements. Additionally, JavaScript or server-side scripting languages (e.g., PHP, Python, etc.) are often used to handle form submissions and perform further processing on the server.