What is HTML Form?
The HTML <form> element represents a document section containing interactive controls for submitting information.
Common HTML input Elements and attributes
Text: For general input
Email: For email addresses
Password: For password
Number: For numeric input
Placeholder: Defines what type of data to enter in a particular input field
Label: describe what each input field is for.
Action: Denotes where form data will be submitted
Methods:
a. GET: form data appended to the action URL with a ? separator.
b. POST: form data sent as the request body.
Let’s create a demo form:
Name field label and input is used to enter simple text
<label for="Name">Name</label> <input type="Text"/>
Email field with placeholder attribute to enter email
<label for="Name">Email</label> <input type="email" placeholder="Exmaple@email.com" />
Password field to enter your secret password
<label for="Password">Password</label> <input type="password" />
Using radio Buttons to select gender
<label for="Gender">Gender</label> <input type="radio" name="Gender" id="Gender" value="Male" />Male <input type="radio" name="Gender" id="Gender" value="Female" />Female
Using date value in type attribute to pick DOB
<label for="Date Of Birth">Date Of Birth</label> <input type="date" name="Date Of Birth" id="Date Of Birth" value="" />
Select events using checkbox attributes
<label for="Events participating">Events participating</label> <input type="checkbox" name="Events participating" id="Events participating" value="" /> 100m race <input type="checkbox" name="Events participating" id="Events participating" value="" /> 200m race <input type="checkbox" name="Events participating" id="Events participating" value="" /> 100m relay race
Combining All these
<!DOCTYPE html>
<html>
<head>
<title>Hello, World!</title>
<link rel="stylesheet" href="styles.css" />
</head>
<body>
<form action="/submission" method="get" accept-charset="utf-8">
<label for="Name">Name</label>
<input type="Text"/>
<br />
<label for="Name">Email</label>
<input type="email" placeholder="Example@email.com" />
<br />
<label for="Password">Password</label>
<input type="password" />
<br />
<label for="Gender">Gender</label>
<input type="radio" name="Gender" id="Gender" value="Male" />Male
<input type="radio" name="Gender" id="Gender" value="Female" />Female
<br />
<label for="Date Of Birth">Date Of Birth</label>
<input type="date" name="Date Of Birth" id="Date Of Birth" value="" />
<br />
<label for="Events participating">Events participating</label>
<input type="checkbox" name="Events participating" id="Events participating" value="" /> 100m race
<input type="checkbox" name="Events participating" id="Events participating" value="" /> 200m race
<input type="checkbox" name="Events participating" id="Events participating" value="" /> 100m relay race
</form>
</body>
</html>
Conclusion
As a beginner, practice creating various types of forms to become familiar with their structure and functionality. Whether you're building a simple contact form or a complex registration form, understanding HTML forms is a crucial step in your web development journey. Happy coding!