Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

just a question/gripe

Status
Not open for further replies.

jemminger

Programmer
Joined
Jun 25, 2001
Messages
3,453
Location
US
why in the world does php require you to name form field arrays such as checkboxes like "myBox[]" instead of just "myBox"??? every other server-side language on planet earth does not impose this absurd requirement.

=========================================================
-jeff
try { succeed(); } catch(E) { tryAgain(); }
 
You don't have to name them that way.

All naming checkbox fields in the form "myBox[]" does is make all the values from those checkboxes appear in a single array in the PHP script to which the form submits the data. If you don't need the convenience of being able to loop through all those variables in a foreach() or for() loop, name them whatever you want.

Being able to place a set of checkbox values in an scripting-language array is actually a feature that PHP has that most other CGI scripting languages do not.

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
thanks for your reply.

>> "Being able to place a set of checkbox values in an scripting-language array is actually a feature that PHP has that most other CGI scripting languages do not."

i'm not so sure about that - in ASP for example, if I have three checkboxes all named "foo", on the server side I can loop through them array-style using
Code:
for x = 0 to request("foo").count
  rem etc...
next
so are you saying that if i don't name them with [] in php, i can't use a loop to iterate them? how do i access the above example in php, where i have three elements just named "foo"? in my testing, all i could access in php was the last of the checked checkboxes.



=========================================================
-jeff
try { succeed(); } catch(E) { tryAgain(); }
 
Yes, if you wish to loop through multiple checkboxes of the same name, they must be named of the form "foo[]". Remove the braces and you'll only get the last one.

What I meant is that you could name your checkboxes "foo", "bar" and "baz". You still can't loop through them. Well, you can, but not as easily.

I'm not familiar with the nitty gritty details of VBScript/ASP. If it'll allow three checkboxes named "foo", so be it. However, an arbitrary design decision to either require the braces or not is not an absurd requirement -- it's a design choice. The authors of PHP thought it was necessary that you explicitly inform the parser that the multiple checkboxes are related and should be treated as an array.

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top