Are there any rules one needs to follow when naming POST variables in a form or GET variables in a query string?
Thanks-
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
TO answer the question literally, there really are no “rules” I’m aware of for naming
$_POSTand$_GETarray keys in php. It’s an array like any other. Take a look at this working example on Codepad:In the case of form input names, they just have to be legal HTML “name” attributes (see below). However, in practice, a lot of unusual characters will actually work. Keep in mind that this doesn’t mean it’s a good idea. Different servers (and probably different browsers) will act differently with some characters like spaces for instance.
As Tadeck has noted, duplicate keys will be overwritten by the last one when reading, but using
brackets[]will solve this on the client side by turning the variable into an array.As far as naming conventions and best practices, there isn’t a lot of room. It’s suggested that you stick to A-Z a-z 0-9, dashes, and underscores. Although Ajay has suggested using database column names for form input names as a matter of convenience, many people will tell you that it is bad practice to expose information about your database to the public. I think invertedlambda probably has the closest answer here to the question, and Tadeck has the closest answer as far as best practices.
Regarding HTML “name” attributes: http://www.w3.org/TR/html4/types.html#h-6.2
Maybe someone can enlighten me as to whether or not the above document is a rule or a recommendation, I’m by no means an expert on this subject. I seem to have no issues breaking some of these rules in practice. I also have no problem validating this example document as XHTML strict:
Paste it into the validator, it will pass.
One more best practice to add: Make your form input names or get/post keys meaningful, as with every other naming convention of course. Don’t use
input1and$_GET['param']. Use names that describe the meaning, likelast_nameor$_GET['sort_order'].