HTML forms are elements that collect user input on a webpage and send it to a server for processing. They are essential for tasks like login, registration, search, or submitting feedback.
Key Components of a Form
<form>Tag- The container for all input elements.
- Attributes:
action→ URL where form data is sentmethod→ HTTP method (GETorPOST)
- Input Elements
<input>→ text, password, checkbox, radio, email, etc.<textarea>→ multi-line text input<select>→ dropdown menu with<option><button>→ submit or reset
- Labels
<label>improves accessibility and connects text to input fields.
How Forms Work
- User fills out the inputs.
- When the user clicks submit or presses Enter, the browser collects all input data.
- The browser sends the data to the URL in the
actionattribute using the specifiedmethod. - Server processes the data and sends back a response, which can update the page or redirect the user.
Example
<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>
<button type="submit">Submit</button>
</form>
nameattributes → used as keys when sending data to the server.requiredattribute → ensures user fills out the field.
💡 In Short:
HTML forms collect user input and submit it to a server for processing. Inputs, labels, and buttons work together to make forms interactive, accessible, and functional.