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
<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:
username=Oleh&password=12345That 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:
<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=valuepairs; - it joins them into a string and sends it to the server via
GETorPOST.
Example with a checkbox:
<input type="checkbox" name="subscribe" value="yes" checked>On submission:
subscribe=yes4. Interaction with JavaScript
The name attribute makes it easy to read field values:
<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
| Element | Why 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:
| Parameter | Value |
|---|---|
| Attribute | name |
| Purpose | Sets the field name when sending data |
| What it does | Links the field's value to a key on submission |
| Without it | The data will not end up in the request |
| Applies to | Any 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 readyA concise answer to help you respond confidently on this topic during an interview.