1. What Are HTML Forms?
Forms allow users to enter and submit data on websites. Some of the common uses of forms include and are not limited to;
-
Login/signup pages
-
Contact forms
-
Surveys
-
Search boxes
2. Basic Form Structure
Key attributs in forms
Attribute |
Purpose |
---|---|
|
Where to send form data (URL) |
|
HTTP method ( |
|
Identifies the form |
3. Form Elements(Most common)
Text Inputs
sample code:
<label>Username:
<input type="text" name="username" placeholder="Enter username">
</label>
Password Field
<label>Password:
<input type="password" name="pwd">
</label>
Email Input
<input type="email" name="user_email" required>
(Automatically validates email format)
Radio Buttons
<fieldset>
<legend>Gender</legend>
<label><input type="radio" name="gender" value="male"> Male</label>
<label><input type="radio" name="gender" value="female"> Female</label>
</fieldset>
Check boxes
<label>
<input type="checkbox" name="hobbies" value="sports"> Sports
</label>
Dropdown Menu
<label>Country:
<select name="country">
<option value="us">USA</option>
<option value="uk">UK</option>
</select>
</label>
Text Area
<label>Message:
<textarea name="msg" rows="4" cols="30"></textarea>
</label>
Submit Button
<button type="submit">Register</button>
4. Important Form Attributes
Attribute |
Example |
Purpose |
---|---|---|
|
|
Makes field mandatory |
|
|
Gray hint text |
|
|
Pre-filled content |
|
|
Prevents editing |