HTML Form Element
HTML forms are a way to collect user input on a webpage. They allow users to submit data to a server for processing. The <form>
element is the container for all the input elements that make up a form.
Basic Structure
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>Basic Form Example</title>
</head>
<body>
<form action="/submit" method="post">
<label for="name">Name:</label>
<input type="text" id="name" name="name">
<label for="email">Email:</label>
<input type="email" id="email" name="email">
<label for="age">Age:</label>
<input type="number" id="age" name="age">
<input type="submit" value="Submit">
</form>
</body>
</html>
Key Attributes
- action: Specifies the URL where the form data will be sent. In this example, data will be sent to
/submit
. - method: Defines the HTTP method to use when sending form data. Common methods are
GET
andPOST
.GET
appends the form data to the URL, whilePOST
sends the data in the body of the request.
Common Input Types
<input type="text">
: A single-line text input.<input type="password">
: A single-line input that hides the characters.<input type="email">
: A field for email addresses, often includes validation.<input type="number">
: A field for numerical input.<textarea>
: A multi-line text input.
Example with More Input Types
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Advanced Form Example</title>
</head>
<body>
<form action="/submit" method="post">
<label for="username">Username:</label>
<input type="text" id="username" name="username" required>
<label for="password">Password:</label>
<input type="password" id="password" name="password" required>
<label for="gender">Gender:</label>
<select id="gender" name="gender">
<option value="male">Male</option>
<option value="female">Female</option>
<option value="other">Other</option>
</select>
<label for="bio">Bio:</label>
<textarea id="bio" name="bio" rows="4" cols="50"></textarea>
<label for="subscribe">
<input type="checkbox" id="subscribe" name="subscribe">
Subscribe to newsletter
</label>
<fieldset>
<legend>Preferred Contact Method:</legend>
<label>
<input type="radio" name="contact" value="email">
Email
</label>
<label>
<input type="radio" name="contact" value="phone">
Phone
</label>
</fieldset>
<input type="submit" value="Submit">
</form>
</body>
</html>
Explanation of Additional Elements
<select>
: Creates a dropdown menu.<textarea>
: Creates a multi-line text input area.<input type="checkbox">
: Creates a checkbox input.<input type="radio">
: Creates radio buttons for selecting one option from a group.<fieldset>
: Groups related elements in a form.<legend>
: Provides a caption for the<fieldset>
.
Forms can be styled with CSS and made interactive with JavaScript to enhance user experience.
At Online Learner, we're on a mission to ignite a passion for learning and empower individuals to reach their full potential. Founded by a team of dedicated educators and industry experts, our platform is designed to provide accessible and engaging educational resources for learners of all ages and backgrounds.
Copyright 2023-2025 © All rights reserved.