HTML Forms – Are name and id required?

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?

2

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

0

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.

0

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.

4

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

HTML5 quote

Naming form controls: the name attribute confirms that it is not mandatory:

The name content attribute gives the name of the form control, as used in form submission and in the form element’s elements object. If the attribute is specified, its value must not be the empty string or isindex.

What happens if it is not specified?

In Chromium 75 where I tested with nc, it just does not get sent:

<!doctype html>
<html lang=en>
    <head>
        <meta charset=utf-8>
        <title>Min sane</title>
    </head>
    <body>
        <form action="http://localhost:8000" method="post">
            <p><input type="text" name="key" value="default value"></p>
            <p><button type="submit">Submit</button></p>
        </form>
    </body>
</html>

sends:

key=default+value

but without name it simply doesn’t.

Both pass the HTML validator however.

Why would you want an input without name?

JavaScript can still access the value and do something with it.

2

name is needed for POST and GET… but not idid is used for client-side processing.

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 stuff like that. It is also possible only with the name. So Name is more important. Giving an id makes it look standardised.

2

Just want to add that the name attribute is required if you’re working with radio elements.

With the name attribute, the radios in a radio group will belong together. Example below:

<p>Colors</p>
<div>
   <input type="radio" name="colors" value="red" id="radio-red"><label for="radio-red">Red</label>
   <input type="radio" name="colors" value="blue" id="radio-blue"><label for="radio-blue">Blue</label>
</div>

<p>Pets</p>
<div>
   <input type="radio" name="pets" value="cats" id="radio-cats"><label for="radio-cats">Cats</label>
   <input type="radio" name="pets" value="dogs" id="radio-dogs"><label for="radio-dogs">Dogs</label>
</div>

Link for you to play with: https://codepen.io/bj-rn-andreasson-jogmark/pen/PoQprja

name is required, and id is not that important. However, id is used to associate labels to common form input fields like radio button, textboxes, etc.

8

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật