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?
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?
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.
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, 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">
name
is needed for post
and get
... but not id
... id
is used client side processing...
name
is the attribute that determines the "variable name" when doing a post. id
is used for javascript purposes etc.
There are no required attributes for an input element.
http://w3schools.com/tags/tag_input.asp - w3schools always has great information as well.
name is required, id is not that important. However, id is used to associate labels to common form input fields like radio button, textboxes, etc.
Name is required so that you can post or get the values in the next page. Id is required for you to do manipulations with css and stuffs like that. It is also possible only with the name. So Name is more important. Giving an id makes it look standardised.
name is used for POST and GET.
id is used for styling.
class is used for applying the same style to a bunch of elements that are of the same "class".
That's how I memorize them.