Richard Knop
Richard Knop

Reputation: 83695

Multiple Forms With Input Fields With The Same Name Attribute? Good Or Bad?

Is it an acceptable practice to have multiple HTML forms on a page with input fields that share the same name attribute? For example, the page contains a listing of all players and users are allowed to vote for the best player so next to each player card there is this form:

<form class="vote-for-player" enctype="application/x-www-form-urlencoded" method="post" action="/index/vote-for-best-player">
    <input type="hidden" name="player_id" value="1" />
    <input type="submit" name="vote_for_player" value="Vote" class="input-submit" />
</form>

Value attribute of the hidden input field is different for each form, of course.

Let's say there are 20 forms like this on the page so that means 20 input fields with the name equal to "player_id". If I pass that page through HTML validator, it is valid even with the XHTML 1.0 Strict doctype. But is this an acceptable practice from web standards or accessibility perspective?

One thing I know for sure, it makes the server side processing of the page easier as I just need to load value from one POST field called player_id.

Upvotes: 27

Views: 25710

Answers (3)

svg
svg

Reputation: 509

yes that's perfectly fine.. in fact i personally feel that its really a good practice to do so as it turns relatively pretty handy for developers to work with relatively less names as compared to bunch of them.. moreover when elements are on different forms it dose not cause name space collision in any way since to identify elements in js by name we use both form name as well as input field name so it dose not cause any issues...

Upvotes: 2

Greg
Greg

Reputation: 156

Agree with above answer. Name is totally ok, and will be passed as response parameter of your form. Different story would be if your input elements would have same id's as well - some browsers might have problems traversing dom of your document.

Again, think of bunch of radio buttons, where users can select gender etc. They must have same name (but different id's)...

Upvotes: 14

cherouvim
cherouvim

Reputation: 31903

Yes it is valid, I do it all the time.

Upvotes: 4

Related Questions