HTML Forms and Controls - Notes and Examples
1. The <form> Element
Forms in HTML are used to collect user input and send it to a server.
Syntax:
<form action="submit_form.php" method="post">
<!-- Form controls -->
</form>
2. Common Form Controls
- Text Input
<input type="text" name="username">
- Password Input
<input type="password" name="pwd">
- Radio Buttons
<input type="radio" name="gender" value="male"> Male
<input type="radio" name="gender" value="female"> Female
- Checkboxes
<input type="checkbox" name="hobby" value="reading"> Reading
<input type="checkbox" name="hobby" value="travel"> Traveling
- Dropdown (Select)
<select name="country">
<option value="india">India</option>
<option value="usa">USA</option>
</select>
- Textarea
<textarea name="message" rows="4" cols="30"></textarea>
- File Upload
<input type="file" name="resume">
HTML Forms and Controls - Notes and Examples
- Submit & Reset Buttons
<input type="submit" value="Submit">
<input type="reset" value="Reset">
- Button with JavaScript
<button type="button" onclick="alert('Hello!')">Click Me</button>
3. Complete Example Form
<form action="submit_form.php" method="post">
Name: <input type="text" name="name"><br><br>
Password: <input type="password" name="pwd"><br><br>
Gender:
<input type="radio" name="gender" value="male"> Male
<input type="radio" name="gender" value="female"> Female <br><br>
Hobbies:
<input type="checkbox" name="hobby" value="reading"> Reading
<input type="checkbox" name="hobby" value="sports"> Sports <br><br>
Country:
<select name="country">
<option value="india">India</option>
<option value="usa">USA</option>
</select><br><br>
Message:<br>
<textarea name="message" rows="4" cols="30"></textarea><br><br>
Upload File: <input type="file" name="resume"><br><br>
HTML Forms and Controls - Notes and Examples
<input type="submit" value="Submit">
<input type="reset" value="Reset">
</form>
4. Special HTML5 Attributes
- required: Field must be filled
- placeholder: Hint text inside input
- maxlength: Maximum input length
- min and max: For numbers/dates
- pattern: Regex validation
Example:
<input type="email" name="email" required placeholder="Enter your email">