How does an HTML form send data, and why does every input need a
name?On submit, the browser sends
name=valuepairs for every enabled field that has aname, to theactionURL with themethod. A field without anameis not sent.HTML<!DOCTYPE html> <form action="/signup" method="post"> <label>Email <input type="email" name="email" value="asha@example.com"></label> <label>Nickname <input type="text" value="no name, not sent"></label> <button>Sign up</button> </form>Press the button: only
emailis sent. Defaults:methodisgetandactionis the current URL. See the form page.What is the difference between GET and POST in a form?
GET puts form data in the URL; POST puts it in the request body. Use GET for searches and filters that only read, POST for anything that changes data or sends private or large data.
The trap: POST is not more secure, only HTTPS encrypts. HTML forms support only
get,postanddialog.Why should you use
<label>with form inputs?A
<label>gives an input its accessible name, so screen readers announce it, and clicking the label focuses or toggles the input.HTML<!DOCTYPE html> <p> <input type="checkbox" id="terms"> <label for="terms">I accept the terms (click this text)</label> </p> <p> <input type="checkbox"> No label: only the tiny box is clickable </p> <label>Wrapped label <input type="text" name="city"></label>Connect them with
forand the input'sid, or by nesting the input inside the label. A placeholder does not replace a label. See the label page.What input types does HTML5 provide?
HTML5 added
email,url,tel,number,range,date,time,color,searchand a few more date types.HTML<!DOCTYPE html> <style>label { display: block; margin: 6px 0; }</style> <label>Email <input type="email"></label> <label>Phone <input type="tel"></label> <label>Quantity <input type="number" min="1" max="10" value="2"></label> <label>Volume <input type="range" min="0" max="100"></label> <label>Date <input type="date"></label> <label>Color <input type="color" value="#2563eb"></label>They bring the right control, built-in validation and the right phone keyboard; an unknown type falls back to text. Use
tel, notnumber, for phone numbers. All types are on the input types page.What is the difference between radio buttons and checkboxes?
Radio buttons pick exactly one option from a group; checkboxes are independent. Radios form a group by sharing the same
name.<fieldset> <legend>Plan</legend> <label><input type="radio" name="plan" value="free" checked> Free</label> <label><input type="radio" name="plan" value="pro"> Pro</label> </fieldset> <label><input type="checkbox" name="newsletter" value="yes"> Send me news</label>An unchecked checkbox sends nothing, and a checked one without a
valuesendson. Radios with different names are separate groups. See the radio button page.How does built-in form validation work in HTML5?
Attributes like
required,type="email",pattern,min,maxandminlengthmake the browser check fields on submit and block it with a message, without JavaScript.HTML<!DOCTYPE html> <style>input:user-invalid { outline: 2px solid crimson; }</style> <form> <label>PIN code <input name="pin" required pattern="[0-9]{6}" title="Six digits"> </label> <label>Age <input name="age" type="number" min="18" max="99"></label> <button>Save</button> </form>patternmust match the whole value, andsetCustomValidity()adds custom rules. The server must validate again, since anyone can bypass the browser.What is the difference between
readonlyanddisabled?A
readonlyfield cannot be edited but is focusable and is submitted. Adisabledfield cannot be focused and is not submitted.HTML<!DOCTYPE html> <form> <label>Order id <input name="order" value="A-1042" readonly></label> <label>Coupon <input name="coupon" value="SAVE10" disabled></label> <button>Submit</button> </form>Submit and only
orderis sent, the classic bug when a field is disabled to stop editing.readonlyhas no effect on checkboxes, radios or<select>.What is the difference between
<select>and<datalist>?A
<select>restricts the user to its options. A<datalist>only suggests values for a text input; the user can type anything.HTML<!DOCTYPE html> <label>Course <select name="course"> <option value="html">HTML</option> <option value="css" selected>CSS</option> </select> </label> <label>City <input name="city" list="cities" placeholder="Type or pick"> </label> <datalist id="cities"> <option value="Bengaluru"></option> <option value="Hyderabad"></option> <option value="Pune"></option> </datalist>Use
<select>when only listed values are valid and<datalist>for autocomplete with free input.What do you need in a form to upload a file?
An
<input type="file">and, on the form,method="post"withenctype="multipart/form-data". With the default encoding only the file name is sent, not its contents.<form action="/upload" method="post" enctype="multipart/form-data"> <input type="file" name="resume" accept=".pdf,application/pdf"> <input type="file" name="photos" accept="image/*" multiple> <button>Upload</button> </form>acceptonly filters the file picker, so the server must still check type and size.What does the
autocompleteattribute do, and why isautocomplete="off"often ignored?autocompletetells the browser what a field holds (email,tel,postal-code,current-password,new-password,one-time-code) so autofill and password managers fill it correctly.<input name="email" type="email" autocomplete="email"> <input name="password" type="password" autocomplete="current-password"> <input name="otp" inputmode="numeric" autocomplete="one-time-code">Browsers may ignore
autocomplete="off"on login fields on purpose, because blocking password managers pushes users toward weak passwords.
HTML Forms and Inputs Interview Questions
How forms send data, input types, labels, validation and the attributes behind common form bugs.