HTML Forms: Collect and Submit User Input

An HTML form groups controls such as text fields, checkboxes, radio buttons, select boxes, textareas and submit buttons so user-entered data can be sent for processing.

<form action="signupck.php" method="post">
  <label for="first_name">First name</label>
  <input type="text" id="first_name" name="first_name" required>

  <button type="submit">Submit</button>
</form>

The <form> element defines where the form starts and ends. Controls that should be submitted normally need a name attribute.

HTML form sends user input to a processing page
HTML forms collect data; they do not process it by themselves. The receiving page or application must validate and process the submitted values. Browser-side validation improves usability but does not replace server-side validation.

Basic Form Structure Top ↑

<form action="file_name.php" method="post">
  <!-- Form controls go here -->
</form>

The most important form attributes are:

  • action: where the submitted data is sent.
  • method: how the browser sends the form data, usually GET or POST.

If method is omitted, the default is GET.

Choose the Right Form Control Top ↑

Choose the control according to the kind of data the user must enter.

  • Short free text: text input.
  • Password: password input.
  • One choice from a small group: radio buttons.
  • Zero, one or several independent choices: checkboxes.
  • One or more choices from a list: select box.
  • Long multi-line text: textarea.
  • File from the user's device: file input.
  • Suggested values while still allowing custom input: datalist.

The control should make the expected input obvious and should be paired with a clear label.

action Attribute Top ↑

The action attribute identifies the URL that receives the submitted form data.

<form action="signupck.php" method="post">
  ...
</form>

The action can be a relative URL:

<form action="../process/signupck.php" method="post">

or an absolute URL when the form is intentionally submitted to another origin:

<form action="https://www.example.com/process.php" method="post">

If the action is omitted, the form submits to the current document URL.

method Attribute: GET and POST Top ↑

The two common form methods are get and post.

MethodTypical useWhere data appears
GETSearches, filters and requests that do not change server dataURL query string
POSTSubmitting data that creates or changes server-side stateHTTP request body
POST is not encryption. POST keeps submitted values out of the URL, but sensitive data still requires HTTPS. The receiving application must also validate, authorize and safely process the request.

See GET and POST methods in PHP.

When to Use GET Top ↑

GET is useful when the request represents a retrievable resource or view, such as a search or filter.

<form action="search.php" method="get">
  <label for="q">Search</label>
  <input type="text" id="q" name="q">
  <button type="submit">Search</button>
</form>

A submitted value might produce a URL such as:

search.php?q=mysql

Because the parameters are in the URL, GET requests can often be bookmarked and shared.

When to Use POST Top ↑

POST is commonly used for login forms, registration forms, uploads and requests that create or modify server-side data.

<form action="signupck.php" method="post">
  <label for="email">Email</label>
  <input type="email" id="email" name="email" required>
  <button type="submit">Create account</button>
</form>

The values are sent in the request body rather than appended to the URL.

name and id Attributes Top ↑

name identifies the submitted field. id identifies an element in the document and is commonly used to connect a label to its control.

<label for="city">City</label>
<input type="text" id="city" name="city">

When submitted, the field name becomes part of the name/value pair:

city=Hyderabad
A control without a name is generally not included in normal form submission. The id and name may use the same value, but they serve different purposes.

Labels and Accessibility Top ↑

Associate visible text with each form control by using <label>.

<label for="full_name">Full name</label>
<input type="text" id="full_name" name="full_name">

The label improves usability for keyboard, screen-reader and pointer users. Clicking the label also activates many associated controls.

A placeholder is not a replacement for a visible label because placeholder text can disappear after the user starts typing.

Group Related Controls Top ↑

Use <fieldset> and <legend> for a related group such as radio buttons or checkboxes.

<fieldset>
  <legend>Preferred contact method</legend>

  <input type="radio" id="contact_email" name="contact" value="email">
  <label for="contact_email">Email</label>

  <input type="radio" id="contact_phone" name="contact" value="phone">
  <label for="contact_phone">Phone</label>
</fieldset>

Radio buttons that belong to one mutually exclusive group use the same name.

HTML Validation Attributes Top ↑

HTML provides attributes that help browsers check common input requirements before submission.

<label for="email">Email</label>
<input
  type="email"
  id="email"
  name="email"
  maxlength="120"
  autocomplete="email"
  required
>

Common validation-related attributes include required, min, max, minlength, maxlength and pattern.

Client-side validation can be bypassed. Always validate submitted data again on the server.

File Uploads and enctype Top ↑

A form that uploads a file must use POST and multipart/form-data:

<form action="uploadck.php" method="post" enctype="multipart/form-data">
  <label for="document">Choose a file</label>
  <input type="file" id="document" name="document">
  <button type="submit">Upload</button>
</form>

See HTML file upload fields and PHP file upload handling.

HTML Form Elements Top ↑

ElementUse
Text fieldShort text such as a name or address
Hidden fieldSubmit a value that is not presented as an editable control
Password fieldMask characters while the user types a password
TextareaMulti-line text
Radio buttonSelect one option from a mutually exclusive group
CheckboxSelect independent yes/no options or multiple choices
Select / drop-down listSelect one or more predefined options
OptgroupGroup related options inside a select element
DatalistOffer suggestions while still allowing other text values
ButtonTrigger an action or script
Image buttonSubmit a form using an image control
Submit buttonSubmit the form
Reset buttonRestore form controls to their initial values
Hidden fields are not secure storage. Users can inspect and modify submitted hidden values, so the server must not trust them merely because they are not visible on the page.

Buttons and Links Top ↑

TutorialPurpose
Button as hyperlinkNavigate using a button-style link
Image mapLink different areas of an image
HyperlinkCreate normal HTML links
Link windowOpen a link in another browsing context
Parent and child windowsWork with JavaScript-created windows
Links within a pageJump to sections such as FAQ answers
Image areaCreate links on areas of an image map
Email linkCreate a mailto link

Form Applications and Examples Top ↑

ApplicationTutorial
Set focus on a fieldSet Focus
Use two submit buttonsTwo Submit Buttons
Submit to another browsing contextSubmit to New Window
Return to a previous pageHistory Back
Upload a fileFile Upload
Compare selection controlsSelect Options

Video: HTML Form Basics Top ↑

Form Exercise Top ↑

Create a signup form that asks for:

  • First name
  • Second name
  • Gender using radio buttons
  • Class using a select box
  • Languages known using checkboxes: Python, PHP and JavaScript
  • Educational background using a textarea

Each control should have a visible label. Submit the form from signup.html to signupck.php.

Questions and exercises on HTML forms

Continue with collecting HTML form data in PHP.

For a GET-based exercise, see using query-string data as a textbox default and query-string data formatting.

Frequently Asked Questions Top ↑

Q1: What does the HTML form tag do?

It groups form controls and defines how and where their submitted values are sent.

Q2: What is the difference between GET and POST?

GET places submitted parameters in the URL and is useful for retrievable requests such as searches. POST sends data in the request body and is commonly used for requests that create or change server-side state.

Q3: Is POST secure for passwords?

POST keeps values out of the URL but does not encrypt them. Use HTTPS and secure server-side processing for sensitive data.

Q4: Why does a form control need a name attribute?

The name identifies the submitted field. A normal successful control without a name is generally not included in form submission.

Q5: What is the difference between name and id?

The name is used for submitted form data. The id identifies the HTML element and can connect it to a label through the label's for attribute.

Q6: Does HTML validation replace server-side validation?

No. Browser validation improves usability but can be bypassed. The server must validate submitted values again.

Q7: What enctype is required for file upload forms?

Use enctype="multipart/form-data" with method="post".

Next: Text field in an HTML form





plus2net.com







jaki shahrier kanak

20-03-2010

More eaxmples are needed



We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2026   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer