Can a HTML input name contain spaces, commas etc?

2019-08-11 05:55发布

问题:

This question already has an answer here:

  • What characters are allowed in the HTML Name attribute inside input tag? 5 answers

Basically, can I have input names like <input name="total sum(userID2223)" type="text" /> and similar? Are there any rules for that?

回答1:

Yes you can. When PHP reads the form, you will be able to address the data with $POST["total sum(userID2223)"].

Without knowing more about what you are trying to do I can't say for sure, but there's probably a bad design decision happening somewhere.



回答2:

It's not allowed in HTML4: From http://www.w3.org/TR/html401/types.html#type-cdata

ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").

Interestingly, it has long been common to use [] in NAME attributes, even though not allowed by the above; PHP will turn these inputs into arrays in _$GET and $_POST.

It's allowed in HTML5: From http://www.w3.org/TR/html5/forms.html#attr-fe-name

Any non-empty value for name is allowed, but the names "charset" and "isindex" are special

For your application, I would recommend array notation:

<input name="total[2223]" type="text" />

Then in PHP it would be $_POST['total'][2223].



标签: php html forms