An HTML form is designed to collect user or form data using input box, allowing users to submit data to mail or server database. We use the HTML <form> element to create forms.
The HTML <form> tag can include various Input Types, such as text fields, textarea, checkboxes, radio buttons, and dropdowns.
Basic Structure 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>Sample Form</title>
</head>
<body>
<h1>Contact Us</h1>
<form action="submit.php" method="POST">
<label for="name">Name:</label>
<input type="text" id="name" name="name" required><br><br>
<label for="email">Email:</label>
<input type="email" id="email" name="email" required><br><br>
<label for="message">Message:</label>
<textarea id="message" name="message" required></textarea><br><br>
<label for="subscribe">
<input type="checkbox" id="subscribe" name="subscribe" value="yes"> Subscribe to newsletter
</label><br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
Output:
Here are some of the key attributes for controlling data submission.
HTML Form Element
- Action: The
action
attribute specifies where to send the form data, - Method: the
method
attribute defines how to send it (usuallyGET
orPOST
).
GET method in forms
- Usage: Sends data as URL parameters. It appends the form data to the URL, making it visible in the browser's address bar.
<form action="" method="GET">
POST Method
- Usage: Sends data in the body of the HTTP request, keeping it hidden from the URL.
<form action="" method="POST">
Why use POST Method in HTML?
- Suitable for sensitive data (e.g., passwords) and large amounts of data.
- Data is not visible in the URL, making it more secure.
- Data cannot be bookmarked or shared via the URL.
If you have any other questions or need further examples, let us know and upload the question in our forum tapgen.xyz
Learn to code with the world's largest web developer site. Not sure where to begin? HTML is The language for building web pages Learn HTML Tutorial with Tapgen.
=================================