HTML web form code uses the <form> element to collect information such as a name, email address, or message and submit it for processing. A basic form needs labeled fields, a submit button, and a destination or script that knows what to do with the submitted data; the example below gives you a usable starting point.
A Basic HTML Form Example
This form collects a name, email address, and message. Its action and method values illustrate the structure, but the example endpoint must be replaced with a real server-side URL before submissions can be processed.
<form action="/contact" method="post">
<div>
<label for="name">Name</label>
<input id="name" name="name" type="text" autocomplete="name" required>
</div>
<div>
<label for="email">Email</label>
<input id="email" name="email" type="email" autocomplete="email" required>
</div>
<div>
<label for="message">Message</label>
<textarea id="message" name="message" rows="5" required></textarea>
</div>
<button type="submit">Send message</button>
</form>
Paste the code into an HTML page to display the controls. The browser can check that required fields are filled and that the email resembles an email address. It cannot, by itself, deliver the message: a server or form-handling service must receive and process the request.
How Form Elements Work
The <form> element groups controls into one submission. Each field has a purpose defined by its type and attributes:
actionidentifies the URL that receives the submitted form data.methoddetermines how the browser sends it.postputs data in the request body and is commonly used for messages or account details.getappends data to the URL, which is useful for searches and filters that should be shareable.nameis the key used to identify a field in the submitted data. An input without anameis generally not included in the submission.idgives a control a unique identifier on the page. A label’sforvalue should match that identifier.requiredasks the browser to prevent submission when a field is empty. Input types such asemailalso provide basic built-in checks.
A label should describe the information expected, not just repeat a technical field name. Clicking a correctly associated label focuses its control, and assistive technologies can use the association to explain the field.
Choosing Fields and Submission Methods
Use the simplest control that fits the information. Text inputs suit short answers; <textarea> suits longer messages; a set of radio buttons is appropriate when one choice must be selected; and checkboxes suit independent yes-or-no choices. A <select> menu can work for a longer list of options, but avoid hiding a small set of important choices in a menu when visible options would be clearer.
Choose input types that match their data. For example, type="email" offers email-oriented browser behavior, while type="tel" is useful for phone numbers and can present a suitable mobile keyboard. These types do not prove that an address or number is valid, reachable, or owned by the person entering it.
Use get when the submitted values are suitable for appearing in a URL, such as search terms. URLs can be saved, shared, recorded in browser history, or included in logs. Use post for submissions that should not be placed in the URL, but do not treat POST as encryption or a security feature. Protect sensitive data in transit with HTTPS, and handle it securely on the server.
Accessibility, Validation, and Security
Good form code helps people complete the task and helps the receiving system handle input safely. Keep labels visible, group related choices with <fieldset> and <legend> when useful, and make errors clear in text rather than relying only on color. Ensure controls can be reached and operated with a keyboard.
- Use browser validation, such as
required,minlength, or suitable input types, to catch common mistakes early. - Validate submitted values again on the server. Browser checks can be bypassed, so they are not a security boundary.
- Escape or safely handle user-provided text before displaying it elsewhere. Never assume a form submission is trustworthy just because the browser showed no error.
- Collect only information needed for the task, explain what the form is for, and avoid sending private data to an untrusted destination.
For forms that change an account or other protected information, the server may also need authentication, authorization, and protections against forged requests. The exact controls depend on the application; HTML alone cannot provide them.
Common Problems and Fixes
If clicking the button appears to do nothing, check the browser’s developer tools and confirm that the form has a working action and that the destination accepts the chosen method. A path such as /contact is only an example unless your site actually implements it.
- A field is missing from the submission: Add a
nameattribute and check that the control is enabled. Disabled controls are not submitted. - The wrong information is sent: Confirm that each field’s
nameis unique where needed and that radio buttons in one choice group share a name. - The page reloads unexpectedly: A normal form submission often navigates to the response from the server. Preventing that behavior with JavaScript requires writing and maintaining submission and error-handling code.
- The form looks unstyled: HTML creates the structure, not a particular visual design. Add CSS separately, while preserving visible labels, focus indicators, and readable contrast.
HTML Form Template
Use this planning template to note what each field collects and how it will be processed before adding the corresponding HTML. The sample uses fictional details and is illustrative only.
| Field label | HTML name | Input type | Required? | Purpose or notes |
|---|---|---|---|---|
| Name | name | text | Yes | Fictional sample: Alex Example |
| Yes | Fictional sample: alex@example.invalid |
Frequently Asked Questions
Can I make a working form with HTML alone?
HTML can display controls and submit their values, but a destination must receive and process them. That usually means a server-side handler or a form-processing service.
What is the difference between GET and POST?
GET places submitted values in the URL and is often suitable for searches. POST sends values in the request body and is common for other submissions. Neither choice alone makes data private or secure.
Why do form inputs need a name attribute?
The browser uses the name as the key for a field’s value in submitted form data. An input with an id but no name may display and work on the page yet be omitted from submission.
Does the required attribute validate data securely?
No. It provides a convenient browser-side check, but users can bypass it. The receiving server should validate every submitted value independently.
How do I make a form accessible?
Give each control a meaningful, programmatically associated label, use suitable grouping for related options, provide readable error messages, and support keyboard navigation with visible focus.