Forms Β· Astro Tech Blog

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

HTML
<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>
Live Output Window

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:

  1. The browser collects the name-value pairs from all form controls.
  2. It sends them to the URL specified in the action attribute.
  3. The browser navigates to that URL (or processes the response if JavaScript intercepts it).

Targeting the Submission URL

AttributeValueBehaviour
actionURLWhere to send the data
methodget or postHTTP method to use
enctypeapplication/x-www-form-urlencoded, multipart/form-data, text/plainEncoding type for the data
HTML
<!-- 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>
Live Output Window

What Data Gets Sent?

Form data comes from all named form controls β€” elements with a name attribute:

HTML
<input type="text" name="username" value="alice">
Live Output Window

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 with multipart/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:

HTML
<input type="text" placeholder="Enter your full name">
Live Output Window

Text

Single-line text input (default):

HTML
<input type="text" name="fullname" maxlength="50">
Live Output Window

Email

Validates email format automatically:

HTML
<input type="email" name="email" placeholder="user@example.com">
Live Output Window

Password

Masks the input characters:

HTML
<input type="password" name="password">
Live Output Window

Number

Accepts only numeric input:

HTML
<input type="number" name="age" min="0" max="150" step="1">
Live Output Window

Hidden

Sends data to the server without displaying a control to the user:

HTML
<input type="hidden" name="user_id" value="123">
Live Output Window

Default Value

Pre-fills the input using the value attribute:

HTML
<input type="text" name="country" value="United States">
Live Output Window

Required Fields

The required attribute prevents submission if the field is empty:

HTML
<input type="text" name="email" required>
Live Output Window

Format Enforcement

Browser-side validation attributes:

AttributeUsed WithExample
minlength / maxlengthtext, passwordmaxlength="10"
min / maxnumber, date, rangemin="18" max="100"
stepnumber, rangestep="5"
patterntext, search, url, telpattern="[A-Za-z]+"
HTML
<input type="text" pattern="[A-Za-z ]+" title="Letters only">
Live Output Window

Submit

The submit button triggers form submission:

HTML
<input type="submit" value="Send">
<!-- or use a button element -->
<button type="submit">Send</button>
Live Output Window

Other Form Input Types

File Uploads

HTML
<input type="file" name="avatar" accept="image/*" multiple>
Live Output Window
  • accept β€” restricts file types (image/*, .pdf, .doc)
  • multiple β€” allows selecting multiple files

Buttons

HTML
<!-- 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>
Live Output Window

Radio Buttons

Choose one option from a group (same name):

HTML
<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>
Live Output Window

Only the selected radio button’s value is sent. Use checked for a default selection.

Checkboxes

Choose zero or more options from a group:

HTML
<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>
Live Output Window

Each checked checkbox sends its value. Use checked for defaults.

Date and Time Inputs

HTML
<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">
Live Output Window

Color Picker

HTML
<input type="color" name="theme_color" value="#ff0000">
Live Output Window

Range Slider

HTML
<input type="range" name="volume" min="0" max="100" value="50">
Live Output Window

Telephone Input

HTML
<input type="tel" name="phone" placeholder="+1-555-0100">
Live Output Window

URL Input

HTML
<input type="url" name="website" placeholder="https://example.com">
Live Output Window

Textarea Tag

Multi-line text input. Use rows and cols for size:

HTML
<label for="message">Message:</label>
<textarea id="message" name="message" rows="5" cols="40"
placeholder="Write your message here..."
maxlength="500"></textarea>
Live Output Window

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:

HTML
<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>
Live Output Window
  • <optgroup> groups options into labelled categories:
HTML
<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>
Live Output Window
  • multiple attribute allows selecting multiple options (Ctrl+click):
HTML
<select name="skills" multiple size="4">
<option>HTML</option>
<option>CSS</option>
<option>JavaScript</option>
<option>Python</option>
</select>
Live Output Window

Detailed Table with Tag, attributes and attribute values

TagAttributeCommon ValuesDescription
<form>actionURLSubmission endpoint
<form>methodget, postHTTP method
<form>enctypeapplication/x-www-form-urlencoded (default), multipart/form-data (files), text/plainEncoding type
<form>novalidate(boolean)Disable browser validation
<form>target_self, _blank, _parent, _topWhere to display response
<input>typetext, email, password, number, hidden, submit, radio, checkbox, file, date, color, range, tel, url, searchInput type
<input>nametextKey sent to server
<input>valuetextDefault value / sent value
<input>placeholdertextHint text
<input>required(boolean)Must be filled
<input>disabled(boolean)Cannot be interacted with
<input>readonly(boolean)Cannot be edited
<input>min / maxnumber, dateValid range
<input>stepnumberIncrement step
<input>patternregexValidation pattern
<input>acceptMIME type, file extensionAccepted file types
<input>multiple(boolean)Allow multiple files/options
<input>autocompleteon, off, email, name, etc.Auto-fill behaviour
<textarea>rowsnumberVisible height (lines)
<textarea>colsnumberVisible width (characters)
<textarea>maxlengthnumberMax characters
<textarea>wrapsoft, hardLine wrapping
<select>nametextKey sent to server
<select>multiple(boolean)Allow multiple selections
<select>sizenumberNumber of visible options
<option>valuetextSent value
<option>selected(boolean)Pre-selected option
<optgroup>labeltextGroup label