Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
771 views
in Technique[技术] by (71.8m points)

html - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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)...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...