Understanding HTML Forms
The <form> tag is the main container that holds all form fields like input boxes, buttons, and dropdowns.
An HTML form is used to collect data from users.
For Example:
- Registration forms
- Login forms
- Contact forms
- Search boxes
When a user fills in the details, the data is sent to the server for processing. Once processed, the response can be sent back to you via email or another method.
The <form> element
The <form> element is the main container that holds all the input fields and controls.
Users cannot interact with the form without this tag.
For example:
<form>
<!-- All the content of the form is written inside this tag -->
</form>
Important Attributes
- action - This attribute is used to define where the form data will be sent.
<form action="submit.php">When the user clicks the submit button, the data is sent to the submit.php file.
- method - This attribute is used to define how the data will be sent.
There are two methods for sending data:
-
GET Method - In this method, the data is sent through the URL.
It is used for search forms, filters, and senstive data.
For Example:
<form action="submit.php" method="GET">Features:
- Data is visible in the URL.
- It has a limited data size.
- It can be easily bookmarked.
Disadvantage:
- Data is not secure; anyone can see it.
- POST Method - In this method, the data is sent inside the request body (not in the URL).
It is used for login forms, registration, and sensitive data.
For Example:
<form action="submit.php" method="POST">Features:
- Data is hidden from the URL.
- It is more secure than GET.
- It can send large amounts of data.
- It is used for sensitive information (passwords, forms, etc.).
-
GET Method - In this method, the data is sent through the URL.
- name attribute - The name attribute is used in a form to identify the input field when the data is sent.
For Example:
<input type="text" name="username">What will happen in this code?
If the user enters "John" in the input field, the data sent will be:
username=John - placeholder attribute - This attribute is used to define a hint for the input field.
For Example:
<input placeholder="Enter your name">Placeholder defines what the input field is asking the user to enter.
-
Form Example:
<form action="submit.php" method="POST"> <input type="text" name="username" placeholder="Enter your username"> <input type="email" name="email" placeholder="Enter your email"> <input type="password" name="password" placeholder="Enter your password"> </form>- The <form> tag is the main container in which all the form data is collected.
- The <input> tag is used by users to enter data into the form.
Conclusion
HTML forms are one of the most important parts of web development. They help users interact with a website by entering data into input fields.
The <form> tag acts as a container, while different input types help collect various kinds of data such as text, numbers, emails, checkboxes, and many more.