By WebNest Studio Engineering Team
HTML Tutorial
HTML Forms and Submission
Forms are how HTML collects information from users and sends it somewhere for processing — a login, a search box, a checkout page, or a contact form. The <form> element wraps all of these interactive controls and defines where and how their collected data is submitted.
Why it matters
Login, sign-up, search, checkout and contact pages are all forms. They are how users send data to a server. Understanding what action, method and name do explains what actually leaves the browser when the button is pressed, which is essential for working with any back end.
The action and method Attributes
action specifies the URL that receives the submitted form data; if omitted, the form submits to the current page's own URL. method specifies the HTTP method used: GET appends form data as a visible query string in the URL (suitable for searches or non-sensitive filters), while POST sends data in the request body, which is required for anything that changes server state or contains sensitive information like passwords.
The Submission Flow
When a user activates a submit control (typically a <button type="submit"> or <input type="submit">) inside a form, the browser first runs any native validation on the fields (like required or pattern), and if all fields pass, it gathers each control's name/value pair and sends the request to action using method. If validation fails, submission is blocked and the browser highlights the first invalid field.
- Every field that should be submitted needs a
nameattribute — without it, its value is not included in the submitted data. GET— data visible in the URL, bookmarkable, appropriate for searches.POST— data sent in the request body, appropriate for sensitive or state-changing submissions.
Example
A basic contact form submitting via POST
<form action="/submit-contact" method="POST">
<label for="name">Name</label>
<input type="text" id="name" name="name" required>
<label for="message">Message</label>
<textarea id="message" name="message" required></textarea>
<button type="submit">Send Message</button>
</form>
Displays a text field and a textarea with a Send Message button; clicking it validates that both fields are filled in, then sends a POST request to /submit-contact with name and message values in the request body.
Common Mistakes
- Forgetting the name attribute on inputs, so their values are silently excluded from the submitted data.
- Using GET for forms that submit sensitive data like passwords, exposing it in the URL and browser history.
- Omitting the action attribute unintentionally and being surprised the form submits back to the same page.
- Using a plain <div> or <span> with a click handler instead of a real submit button, breaking default form submission and keyboard behavior (like submitting on Enter).
Exercise
Build a contact form that sends its data with the POST method to /contact. It needs a text field named name, a multi-line field named message, each with a label, and a submit button reading Send.
Expected result: a form with two labelled fields and a Send button.
<!-- TODO: form with action and method -->
<!-- TODO: labelled name field -->
<!-- TODO: labelled message field -->
<!-- TODO: submit button -->
Show hints
- A field is submitted only if it has a
nameattribute. - A multi-line field is a
textarea, and a label is connected to its field by matchingforandid.
Show solution
<form action="/contact" method="post">
<p>
<label for="name">Name</label>
<input type="text" id="name" name="name">
</p>
<p>
<label for="message">Message</label>
<textarea id="message" name="message"></textarea>
</p>
<button type="submit">Send</button>
</form>
Quick quiz
1. Where does a form with method="get" put the submitted data?
- A. In the URL, as a query string
- B. In the request body
- C. In a cookie
- D. In local storage
Show answer
A. With POST the data travels in the body of the request.
2. An input has an id but no name. What is sent when the form is submitted?
- A. The id and the value
- B. The value only
- C. Nothing for that input
- D. An error is raised
Show answer
C. The name is the key under which the value is sent. Without it the field is left out.
3. What is the default type of a button element inside a form?
- A. button
- B. submit
- C. link
- D. reset
Show answer
B. A button meant only to run JavaScript should be given type="button" so that it does not submit the form.
Interview questions
What is the difference between GET and POST for a form?
Show answer
With GET, the field values are appended to the URL as a query string. The result can be bookmarked and shared and is right for searches and filters, but the data is visible, limited in length and stored in browser history, so it must not be used for passwords or for anything that changes data. With POST, the values are sent in the request body; it is used for logins, sign-ups and any submission that creates or changes something.
What happens when a form is submitted?
Show answer
The browser runs its built-in validation, and stops if a field is invalid. Otherwise it collects each successful control, meaning those with a name that are not disabled, as name and value pairs, encodes them, and sends an HTTP request to the URL in action using the given method. The browser then loads the response as a new page, unless JavaScript handled the submit event and called preventDefault().
Key Points to Remember
- The form element wraps input controls and defines where (action) and how (method) they are submitted.
- GET exposes data in the URL; POST sends it in the request body — use POST for sensitive or state-changing data.
- Every field needs a name attribute to be included in the submitted data.
- Native validation runs before submission and blocks it if required fields or patterns fail.
Practice in the playground
Write and run your own code for this topic, or start from the exercise or an example. Shows a live preview of your HTML, CSS and JavaScript. Your code for each topic is kept in this browser.
Run the examples
Change an input, predict the result, then compare it with the output. Explain why the result changes.
Written by
WebNest Studio Engineering Team
Practical lessons from developers working with Java, Python, React, databases and production systems.
Led by Vansh Duggal, Founder, WebNest Studio · Full-stack & software engineering