Forms in HTML — Interview Questions & Answers
Introduction to Forms
Ques: What are HTML forms used for?
Ans: Forms collect user input and send it to a server for processing.
Example:
<form action="/submit" method="post">
<label>Name:</label>
<input type="text" name="username">
<input type="submit" value="Send">
</form>
Form Attributes
| Attribute | Description | Example |
|---|---|---|
action | Where to send form data | <form action="/login"> |
method | HTTP method (GET / POST) | <form method="post"> |
target | Where to display response | _self, _blank |
autocomplete | Enables/disables auto-fill | autocomplete="off" |
novalidate | Disables browser validation | <form novalidate> |
Form Methods
Ques: What is the difference between GET and POST?
| GET | POST |
|---|---|
| Appends data to URL | Sends data in body |
| Not secure | More secure |
| Used for fetching data | Used for submitting data |
| Limited data size | No size limit |
Example using POST:
<form action="/register" method="post">
<input type="text" name="user">
<input type="submit" value="Register">
</form>
Form Controls
| Tag | Purpose |
|---|---|
<input> | Single-line input |
<textarea> | Multi-line input |
<select> | Dropdown menu |
<button> | Clickable button |
<label> | Describes input field |
<fieldset> / <legend> | Group related fields |
Input Types
Ques: List some common input types.
Ans: text, password, email, number, date, checkbox, radio, file, submit, reset, button, range, color.
Example:
<form>
<input type="text" name="user" placeholder="Enter name"><br>
<input type="password" name="pass"><br>
<input type="email" name="email"><br>
<input type="submit" value="Send">
</form>
Checkbox and Radio Buttons
Ques: How do you create checkboxes?
<input type="checkbox" name="hobby" value="music"> Music
<input type="checkbox" name="hobby" value="travel"> Travel
Ques: How do you create radio buttons?
<input type="radio" name="gender" value="male"> Male
<input type="radio" name="gender" value="female"> Female
Dropdowns and Text Areas
Ques: How do you create a dropdown menu?
<select name="country">
<option value="in">India</option>
<option value="us">USA</option>
<option value="uk">UK</option>
</select>
Ques: How do you create a multi-line input field?
<textarea name="message" rows="4" cols="30"></textarea>
Labels and Accessibility
Ques: What is the purpose of <label>?
Ans: Improves accessibility by linking text to inputs.
<label for="email">Email:</label>
<input type="email" id="email">
Grouping Inputs
Ques: What are <fieldset> and <legend> used for?
<fieldset>
<legend>Personal Info</legend>
<input type="text" name="name">
<input type="email" name="email">
</fieldset>
HTML5 Input Enhancements
Ques: List some new HTML5 input types.
Ans: date, time, number, range, color, email, url, tel.
Ques: What are some useful HTML5 attributes for validation?
| Attribute | Purpose |
|---|---|
required | Field must be filled |
min, max | Limit values |
pattern | Regex validation |
maxlength | Max characters |
step | Increment for numbers |
Form Validation
Ques: How does HTML5 perform form validation?
Ans: Automatically checks built-in constraints before submission (e.g., required fields, pattern match).
Example:
<form>
<input type="email" required>
<input type="submit">
</form>
Buttons
Ques: What are the different button types?
| Type | Action |
|---|---|
submit | Submits form |
reset | Clears form |
button | Custom JS action |
Example:
<button type="submit">Send</button>
<button type="reset">Clear</button>
File Upload
Q18: How to upload a file in HTML?
<form enctype="multipart/form-data">
<input type="file" name="resume">
</form>
enctype="multipart/form-data" is required for file uploads.
Hidden Fields
Ques: What is the purpose of hidden inputs?
Ans: To store data not visible to users (e.g., user IDs).
<input type="hidden" name="userid" value="1234">
Advanced Form Features
Ques: How to auto-fill suggestions in inputs?
<input list="browsers">
<datalist id="browsers">
<option value="Chrome">
<option value="Firefox">
<option value="Edge">
</datalist>
Ques: Is HTML form validation enough for security?
Ans: No, you must always validate on the server side too.
Ques: Why shouldn’t you use GET for password forms?
Ans: Because data appears in the URL and can be logged or cached.
