Skip to main content

What does name do on a form field?

The name attribute on a form field defines the variable name under which the browser sends that field's value to the server when the form is submitted.

It works like a key in a "key = value" pair, where name is the key, and the value entered by the user is the data attached to it.


1. Example

html
<form action="/submit" method="post"> <input type="text" name="username" value="Oleh"> <input type="password" name="password" value="12345"> <button type="submit">Log in</button> </form>

On submission, the browser sends the data to the server like this:

javascript
username=Oleh&password=12345

That is:

  • name="username" -> key "username",
  • value="Oleh" -> value "Oleh".

2. Without name, the data is not sent

If a field has no name attribute, it does not end up in the request; the server simply will not know the user entered anything.

Example:

html
<input type="text" placeholder="Name">

Even if the user types text, it will not be sent when the form is submitted.


3. How it works

  • The browser collects every form element that has name;
  • it builds name=value pairs;
  • it joins them into a string and sends it to the server via GET or POST.

Example with a checkbox:

html
<input type="checkbox" name="subscribe" value="yes" checked>

On submission:

javascript
subscribe=yes

4. Interaction with JavaScript

The name attribute makes it easy to read field values:

html
<form id="form"> <input type="text" name="email" value="test@example.com"> </form> <script> const email = document.forms["form"]["email"].value; console.log(email); // "test@example.com" </script>

5. When name matters most

ElementWhy name is needed
<input>To send the entered value
<textarea>To pass the text
<select>To send the chosen option
<input type="radio">To group them together
<input type="checkbox">To identify the chosen item

Summary:

ParameterValue
Attributename
PurposeSets the field name when sending data
What it doesLinks the field's value to a key on submission
Without itThe data will not end up in the request
Applies toAny form elements (input, textarea, select)

In simple terms: name is a "tag" for a field that lets the server understand which data belongs to what. If a form is a questionnaire, then name is the name of a row on that questionnaire, and the value entered by the user is the answer to that question.

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.