html5 - Do input names have to be unique in the same form? -
i have form multiple fieldset
s. each fieldset
has multiple inputs
, of logically share name
attribute.
i have looked on mdn input name , html 5 spec no luck. section 4.10.19.1 of html 5 form spec not mention uniqueness requirement though.
for example:
<fieldset name="attendee"> <input name="full-name"> </fieldset> <fieldset name="next-of-kin"> <input name="full-name"> </fieldset>
each input
name unique within fieldset
, duplicated within form
. valid?
no, name
attributes not required unique. have name attribute similar in multiple input fields (the whole principle behind radio buttons example). modern browsers see array of information when submitting form. give example in used php parse information, point stands in other programming languages.
given example:
<fieldset name="attendee"> <input name="full-name"> </fieldset> <fieldset name="next-of-kin"> <input name="full-name"> </fieldset>
if var_dump()
depending on method post/get, see browser remembering last value recorded full-name
attribute. if first input john doe
(under attendee
fieldset) , second input john green
(under next-of-kin
fieldset), browser remember john green
regardless of method. if use method url contain both of full-name
attributes, not actual $_get
array itself.
if want record both names can edit code to:
<fieldset name="attendee"> <input name="full-name[]"> </fieldset> <fieldset name="next-of-kin"> <input name="full-name[]"> </fieldset>
by using []
browser knows not remember last value of attribute. if var_dump()
regardless of method should see:
array(1) { ["full-name"]=> array(2) { [0]=> string(8) "john doe" [1]=> string(10) "john green" } }
if chance want more specific (since in 1 of comments mentioning using in rest api), can edit code this:
<fieldset name="attendee"> <input name="full-name[attendee]"> </fieldset> <fieldset name="next-of-kin"> <input name="full-name[next-of-kin]"> </fieldset>
now if submit form, regardless of method, following data-structure:
array(1) { ["full-name"]=> array(2) { ["attendee"]=> string(8) "john doe" ["next-of-kin"]=> string(10) "john green" } }
it simple here call json_encode()
on array , actual json object(like 1 bellow) can use api:
{"full-name":{"attendee":"john doe","next-of-kin":"john green"}}
Comments
Post a Comment