Forms in HTML
Forms collect user input and send it to a server for processing. They are the foundation of user interaction on the web β login pages, search bars, registration forms, checkout pages, and surveys all use forms.
Basic Structure
<form action="/submit" method="post">
<label for="name">Name:</label>
<input type="text" id="name" name="name" required>
<label for="email">Email:</label>
<input type="email" id="email" name="email">
<button type="submit">Submit</button>
</form> A <form> wraps one or more form controls (inputs, buttons, selects, textareas) and defines how the data is sent.
Default Behavior
When a form is submitted:
- The browser collects the name-value pairs from all form controls.
- It sends them to the URL specified in the
actionattribute. - The browser navigates to that URL (or processes the response if JavaScript intercepts it).
Targeting the Submission URL
| Attribute | Value | Behaviour |
|---|---|---|
action | URL | Where to send the data |
method | get or post | HTTP method to use |
enctype | application/x-www-form-urlencoded, multipart/form-data, text/plain | Encoding type for the data |
<!-- GET method β data in URL (search forms) -->
<form action="/search" method="get">
<input type="search" name="q">
<button>Search</button>
</form>
<!-- POST method β data in body (login, registration) -->
<form action="/login" method="post">
<input type="text" name="username">
<input type="password" name="password">
<button>Log In</button>
</form>
<!-- File uploads require multipart encoding -->
<form action="/upload" method="post" enctype="multipart/form-data">
<input type="file" name="avatar">
<button>Upload</button>
</form> What Data Gets Sent?
Form data comes from all named form controls β elements with a name attribute:
<input type="text" name="username" value="alice"> This sends: username=alice
Controls that send data:
<input>(text, email, password, hidden, number, etc.)<textarea><select>(dropdown menus)<input type="radio">(sends value of selected option)<input type="checkbox">(sends value if checked)<input type="file">(sends file content withmultipart/form-data)
Input Tag
The <input> tag is the most versatile form control. Its type attribute determines its appearance and behaviour.
Placeholder
Shows hint text inside the field that disappears when typing:
<input type="text" placeholder="Enter your full name"> Text
Single-line text input (default):
<input type="text" name="fullname" maxlength="50"> Validates email format automatically:
<input type="email" name="email" placeholder="user@example.com"> Password
Masks the input characters:
<input type="password" name="password"> Number
Accepts only numeric input:
<input type="number" name="age" min="0" max="150" step="1"> Hidden
Sends data to the server without displaying a control to the user:
<input type="hidden" name="user_id" value="123"> Default Value
Pre-fills the input using the value attribute:
<input type="text" name="country" value="United States"> Required Fields
The required attribute prevents submission if the field is empty:
<input type="text" name="email" required> Format Enforcement
Browser-side validation attributes:
| Attribute | Used With | Example |
|---|---|---|
minlength / maxlength | text, password | maxlength="10" |
min / max | number, date, range | min="18" max="100" |
step | number, range | step="5" |
pattern | text, search, url, tel | pattern="[A-Za-z]+" |
<input type="text" pattern="[A-Za-z ]+" title="Letters only"> Submit
The submit button triggers form submission:
<input type="submit" value="Send">
<!-- or use a button element -->
<button type="submit">Send</button> Other Form Input Types
File Uploads
<input type="file" name="avatar" accept="image/*" multiple> acceptβ restricts file types (image/*,.pdf,.doc)multipleβ allows selecting multiple files
Buttons
<!-- Submit the form (default) -->
<button type="submit">Save</button>
<!-- Reset all fields to their default values -->
<button type="reset">Clear</button>
<!-- Regular button β does nothing by default -->
<button type="button" onclick="alert("Clicked!")">Click Me</button> Radio Buttons
Choose one option from a group (same name):
<fieldset>
<legend>Gender</legend>
<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>
<input type="radio" id="other" name="gender" value="other" checked>
<label for="other">Other</label>
</fieldset> Only the selected radio buttonβs value is sent. Use checked for a default selection.
Checkboxes
Choose zero or more options from a group:
<fieldset>
<legend>Interests</legend>
<input type="checkbox" id="coding" name="interests" value="coding">
<label for="coding">Coding</label>
<input type="checkbox" id="design" name="interests" value="design">
<label for="design">Design</label>
<input type="checkbox" id="music" name="interests" value="music" checked>
<label for="music">Music</label>
</fieldset> Each checked checkbox sends its value. Use checked for defaults.
Date and Time Inputs
<input type="date" name="birthday">
<input type="time" name="appointment">
<input type="datetime-local" name="meeting">
<input type="month" name="billing_month">
<input type="week" name="event_week"> Color Picker
<input type="color" name="theme_color" value="#ff0000"> Range Slider
<input type="range" name="volume" min="0" max="100" value="50"> Telephone Input
<input type="tel" name="phone" placeholder="+1-555-0100"> URL Input
<input type="url" name="website" placeholder="https://example.com"> Textarea Tag
Multi-line text input. Use rows and cols for size:
<label for="message">Message:</label>
<textarea id="message" name="message" rows="5" cols="40"
placeholder="Write your message here..."
maxlength="500"></textarea> Important: <textarea> is NOT a void element β it needs a closing tag. Any whitespace between the tags appears in the textarea.
Select Tag
Creates a dropdown menu:
<label for="country">Country:</label>
<select id="country" name="country">
<option value="">β Select β</option>
<option value="us">United States</option>
<option value="uk">United Kingdom</option>
<option value="jp" selected>Japan</option>
</select> <optgroup>groups options into labelled categories:
<select name="car">
<optgroup label="German">
<option value="bmw">BMW</option>
<option value="audi">Audi</option>
</optgroup>
<optgroup label="Japanese">
<option value="toyota">Toyota</option>
<option value="honda">Honda</option>
</optgroup>
</select> multipleattribute allows selecting multiple options (Ctrl+click):
<select name="skills" multiple size="4">
<option>HTML</option>
<option>CSS</option>
<option>JavaScript</option>
<option>Python</option>
</select> Detailed Table with Tag, attributes and attribute values
| Tag | Attribute | Common Values | Description |
|---|---|---|---|
<form> | action | URL | Submission endpoint |
<form> | method | get, post | HTTP method |
<form> | enctype | application/x-www-form-urlencoded (default), multipart/form-data (files), text/plain | Encoding type |
<form> | novalidate | (boolean) | Disable browser validation |
<form> | target | _self, _blank, _parent, _top | Where to display response |
<input> | type | text, email, password, number, hidden, submit, radio, checkbox, file, date, color, range, tel, url, search | Input type |
<input> | name | text | Key sent to server |
<input> | value | text | Default value / sent value |
<input> | placeholder | text | Hint text |
<input> | required | (boolean) | Must be filled |
<input> | disabled | (boolean) | Cannot be interacted with |
<input> | readonly | (boolean) | Cannot be edited |
<input> | min / max | number, date | Valid range |
<input> | step | number | Increment step |
<input> | pattern | regex | Validation pattern |
<input> | accept | MIME type, file extension | Accepted file types |
<input> | multiple | (boolean) | Allow multiple files/options |
<input> | autocomplete | on, off, email, name, etc. | Auto-fill behaviour |
<textarea> | rows | number | Visible height (lines) |
<textarea> | cols | number | Visible width (characters) |
<textarea> | maxlength | number | Max characters |
<textarea> | wrap | soft, hard | Line wrapping |
<select> | name | text | Key sent to server |
<select> | multiple | (boolean) | Allow multiple selections |
<select> | size | number | Number of visible options |
<option> | value | text | Sent value |
<option> | selected | (boolean) | Pre-selected option |
<optgroup> | label | text | Group label |