HTML FORMS
HTML forms are used to collect user input and send it to a server for processing.
Basic Form Syntax
<form action="/submit" method="post">
<!-- Form elements go here -->
</form>
- action: URL where the form data is sent.
-
method:
-
GET– Sends data in the URL (used for searches). -
POST– Sends data in the request body (used for sensitive or large data).
-
Common Form Elements
Text Input
<label for="name">Name:</label>
<input type="text" id="name" name="name">
Password
<label for="password">Password:</label>
<input type="password" id="password" name="password">
<input type="email" name="email" required>
Number
<input type="number" name="age" min="1" max="100">
Radio Buttons
<p>Gender:</p>
<input type="radio" id="male" name="gender" value="Male">
<label for="male">Male</label>
<input type="radio" id="female" name="gender" value="Female">
<label for="female">Female</label>
Checkboxes
<input type="checkbox" name="hobby" value="Reading"> Reading
<input type="checkbox" name="hobby" value="Sports"> Sports
Dropdown List
<label for="country">Country:</label>
<select id="country" name="country">
<option value="usa">USA</option>
<option value="uk">UK</option>
<option value="india">India</option>
</select>
Text Area
<label for="message">Message:</label>
<textarea id="message" name="message" rows="4" cols="30"></textarea>
Submit Button
<input type="submit" value="Submit">
Or
<button type="submit">Submit</button>
Complete Example
<!DOCTYPE html>
<html>
<head>
<title>Registration Form</title>
</head>
<body>
<h2>Registration Form</h2>
<form action="/submit" method="post">
<label>Name:</label><br>
<input type="text" name="name" required><br><br>
<label>Email:</label><br>
<input type="email" name="email" required><br><br>
<label>Password:</label><br>
<input type="password" name="password" required><br><br>
<label>Gender:</label><br>
<input type="radio" name="gender" value="Male"> Male
<input type="radio" name="gender" value="Female"> Female
<br><br>
<label>Country:</label><br>
<select name="country">
<option>USA</option>
<option>UK</option>
<option>India</option>
</select>
<br><br>
<label>Message:</label><br>
<textarea name="message"></textarea>
<br><br>
<button type="submit">Register</button>
</form>
</body>
</html>
Frequently Used <input> Types
| Input Type | Purpose |
|---|---|
text | Single-line text |
password | Hidden password input |
email | Email address |
number | Numeric input |
tel | Telephone number |
url | Website URL |
date | Date picker |
time | Time picker |
datetime-local | Date and time |
color | Color picker |
file | File upload |
checkbox | Multiple selections |
radio | Single selection |
range | Slider |
search | Search field |
submit | Submit form |
reset | Reset form |
hidden | Hidden value |
Important Attributes
-
name– Identifies the data sent to the server. -
id– Uniquely identifies an element. -
value– Default value. -
placeholder– Hint text. -
required– Makes a field mandatory. -
readonly– Prevents editing. -
disabled– Disables the field. -
maxlength– Maximum number of characters. -
min/max– Minimum and maximum values. -
pattern– Validates input using a regular expression.
HTML forms are a fundamental part of web development, enabling users to enter data that can be validated in the browser and submitted to a server.
Comments
Post a Comment