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?
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.
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.
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.
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.
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">
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With