DEV Community

Cover image for Common HTML Form Mistakes Beginners Make
Developer Hint
Developer Hint

Posted on Originally published at developerhint.blog AI-assisted

Common HTML Form Mistakes Beginners Make

Forms are where a lot of beginner HTML goes quietly wrong. The page looks fine, the fields appear, the button clicks, and then nothing arrives on the server, or the wrong thing does. Most of these problems come from a handful of small habits, and each one is easy to fix once you know what to look for. Here are the most common HTML form mistakes beginners make, why they cause trouble, and how to avoid them.

1. Leaving out the name attribute

This is the classic reason a form field "does not work". When a form is submitted, the browser sends each field as a name and value pair, and a field without a name is skipped entirely. The field can look perfect on the page and still send nothing.

<!-- Not submitted: no name -->
<input type="text" id="city">

<!-- Submitted as city=... -->
<input type="text" id="city" name="city">

The id and the name do different jobs. The id is for connecting labels, CSS, and JavaScript to the element. The name is what the server receives. Most fields need both.

2. Skipping labels, or using placeholders instead

A field with no label is hard to use. Clicking the text does not focus the field, and screen readers may announce nothing useful. A placeholder does not solve this, because it disappears the moment someone starts typing and is often too faint to read comfortably. Use a real label and connect it by matching its for attribute to the field's id.

<label for="email">Email address</label>
<input type="email" id="email" name="email" placeholder="you@example.com">

You can also wrap the input inside the label, in which case the for attribute is optional. Either way, keep the label visible.

3. Using the wrong input type

Using type="text" for everything works, but it throws away free help from the browser. The email type checks the format and shows a better keyboard on phones, tel brings up a keypad, and date offers a date picker. The reverse mistake is also common. Using type="number" for phone numbers or postal codes is a bad fit, because those are identifiers and not quantities, and the number type can strip leading zeros. Match the type to the data.

4. Forgetting that radio buttons need a shared name

Radio buttons only act as a group when they share the same name. Give each one a different name and the browser lets the visitor select all of them. The value attribute is what gets sent, so each option should have its own.

<label><input type="radio" name="plan" value="free"> Free</label>
<label><input type="radio" name="plan" value="pro"> Pro</label>

5. Expecting unchecked checkboxes to be sent

An unchecked checkbox is not submitted at all, not even as an empty value. If your server logic expects a field called newsletter to always exist, it will break for anyone who left the box unticked. Write your server code so that a missing field means "no". Also give checkboxes a value, since without one the browser sends the default text on.

6. Getting the method and action wrong

If you leave out the method attribute, the form uses GET, which puts the field values into the URL. That is fine for a search box, and a bad idea for anything private, since passwords and personal details end up in browser history, server logs, and shared links. Use method="post" for anything that changes data or contains sensitive information. If you leave out action, the form submits to the current page's URL, which may or may not be what you intended, so set it explicitly.

<form action="/contact" method="post">
  ...
</form>

7. Forgetting how buttons behave

A button inside a form is a submit button by default, even if you meant it as something else. If you add a button to open a menu or toggle a password field, clicking it will submit the form unless you set type="button". This is behind a lot of "my form submits when I press the wrong thing" bugs.

<button type="button" id="show-password">Show password</button>
<button type="submit">Create account</button>

Do not build buttons out of div or span elements either. They will not respond to the keyboard, will not submit forms, and are not announced as buttons by screen readers.

8. Uploading files without the right setup

A file input needs two things on the form or nothing arrives. The form must use method="post", and it must set enctype="multipart/form-data". Without the encoding, the browser sends only the file's name, not its contents.

<form action="/upload" method="post" enctype="multipart/form-data">
  <input type="file" name="photo" accept="image/*">
  <button type="submit">Upload</button>
</form>

9. Mixing up disabled and readonly

Both stop people editing a field, but they behave differently on submit. A disabled field is not submitted at all, and it cannot be focused. A readonly field can be focused and its value is submitted. If you disable a field and then wonder why its value never reaches the server, this is the reason.

10. Nesting forms and repeating IDs

You cannot put one form inside another. Browsers ignore the inner opening tag, so the fields end up belonging to the outer form and may submit together when you did not want them to. Similarly, an id must be unique on the page. If two fields share an id, a label may focus the wrong one, and your CSS and JavaScript will behave unpredictably. This often happens when you copy a block of form HTML and forget to rename the ids.

11. Trusting only the browser to validate

Attributes like required and pattern give visitors quick feedback, but they are not security. Anyone can bypass them, so the server has to validate again. Hidden fields are the same story. Their values can be edited in developer tools, so never trust a hidden price or user id. If you are on WordPress, also remember that forms that change data should use nonces to guard against forged requests.

12. Vague errors and no thought for mobile

Telling someone "Invalid input" does not help them fix it. Say what is wrong and how to correct it, and put the message next to the field. Test your form on a real phone as well, since that is where tiny tap targets, the wrong keyboards, and missing autocomplete attributes become obvious. Adding autocomplete="email", autocomplete="name", and similar values lets browsers fill in fields for people, which can make a form much quicker to complete.

A quick way to see what your form actually sends

When a form misbehaves, the fastest check is to look at the data the browser would submit. The FormData object collects it, so you can print it in the console and spot missing fields at once.

const form = document.querySelector("form");

form.addEventListener("submit", (event) => {
  event.preventDefault();
  console.log([...new FormData(form)]);
});

If a field you expected is not in the output, check for a missing name, a disabled attribute, or an unchecked checkbox. The Network panel in your browser's developer tools shows the same thing from the other side, with the exact request that was sent.

Top comments (0)