Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML Forms - Are name and id required?

Tags:

Why do I need the name and id attributes for <input> form elements?

Which is used for POST data sending and which can I exclude?

like image 884
Robin Rodricks Avatar asked Jun 16 '10 07:06

Robin Rodricks


People also ask

What is id and name in form HTML?

ID is a global attribute and applies to virtually all elements in HTML. It is used to uniquely identify elements on the Web page, and its value is mostly accessed from the frontend (typically through JavaScript or jQuery). name is an attribute that is useful to specific elements (such as form elements, etc.) in HTML.

What is required in HTML form?

Definition and Usage The required attribute is a boolean attribute. When present, it specifies that an input field must be filled out before submitting the form. Note: The required attribute works with the following input types: text, search, url, tel, email, password, date pickers, number, checkbox, radio, and file.

Do you need name attribute in form?

The name attribute is probably the most important attribute of the <input> element. It isn't strictly required for validation, but you should never omit it. When a form is submitted to the server, the data from the form is included in an HTTP request. The data is packaged as a series of name-value pairs.

Should name and id be the same HTML?

If an element has both a name and an id attribute they should be the same (except where the name is shared by multiple elements, such as in a set of radio buttons). As explained at http://stackoverflow.com/questions/10165908/html-5-difference-input-id-and-input-name the name and id attributes are different.


2 Answers

name is used by the server-side. This is necessary if you plan to process the field. id is only so label elements, when clicked and accessed by screen-readers, can trigger/invoke the form controls (inputs and selects).

<form method=POST action="form-processor.php">
    <input name=first_name value=john>
</form>

results in

$_POST = array('first_name' => 'john');

If the method is GET, it's appended to the query string:

http://site-name.com/form-handler.php?first_name=john

It's popular for query string appending with hidden inputs:

<input type="hidden" name="q" value="1">

like image 151
meder omuraliev Avatar answered Sep 30 '22 19:09

meder omuraliev


An id isn't required. Name isn't mandatory either, but the browser will not sent the <input>'s data without it. This is the same for POST and GET.

like image 29
Kobi Avatar answered Sep 30 '22 21:09

Kobi