HTML Forms - Are name and id required?

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.


name is the attribute that determines the "variable name" when doing a post. id is used for JavaScript purposes, etc.


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 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">

Tags:

Html

Forms

Post