php - Several Checkboxes sharing the same name -


according w3c "several checkboxes in form may share same control name. thus, example, checkboxes allow users select several values same property." however, if that, php take last value. example:

<?php if ($_post) { echo "<pre>"; print_r($_post); echo "</pre>"; } ?> <form action="" method = "post"> <input type="checkbox" name="pet" value="dog" />dog<br /> <input type="checkbox" name="pet" value="cat" />cat<br /> <input type="checkbox" name="pet" value="bird" />bird<br /> <input type="checkbox" name="pet" value="iguana" />iguana<br /> <input type="submit" /> </form> 

if submit form, see checked box appears last set. browser sends them all, overwrite each other. so, setting same name several checkboxes can cause problems. has been that? seem remember possible send values array.

i know can add [] @ end of name create array of values:

<?php if ($_post) { echo "<pre>"; print_r($_post); echo "</pre>"; } ?> <form action="" method = "post"> <input type="checkbox" name="pet[]" value="dog" />dog<br /> <input type="checkbox" name="pet[]" value="cat" />cat<br /> <input type="checkbox" name="pet[]" value="bird" />bird<br /> <input type="checkbox" name="pet[]" value="iguana" />iguana<br /> <input type="submit" /> </form> 

but w3c doesn't specify that. don't remember if used [] @ end of name, reason think @ point didn't. there time in past when make work without []?

http://www.w3.org/tr/html401/interact/forms.html#checkbox

that never have worked without [], not in php.

w3c don't specify how query strings handled server-side. (ignoring irrelevant, obsolete corner of cgi spec, relevant php in security hole until recently).

it looks pattern valid markup, not commonly used, reason describe.

a similar pattern is used radio buttons, of 1 can selected @ time. (in fact, giving radio inputs same name how browser knows treat them group). perhaps that's thinking of.


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -