Forum Moderators: open

Message Too Old, No Replies

naming of form fields

         

natty

4:56 pm on Mar 11, 2005 (gmt 0)

10+ Year Member



hi all,

i was just reading a thread in the HTML & Browsers forum (http://www.webmasterworld.com/forum21/9610.htm) and it made me think of something i had come across over the last couple of days.

when i make forms, with checkboxes for instance, i have always, called them the name of the value i want and if the checkbox was not ticked, then request.form will not have them in. i also tend to prefix the elements so that i can easily distinguish them from each other..
so a simple
for each element in request.form
if substr(element,"chk") = 1 then
do stuff with the checkboxes
blah ..
end if
next

seems to do the trick..

however, one of my colleges, does something different..
he calls the checks the same thing(frmCheck), and gives them different values, and then.. does a

ids = request.form("frmCheck")
aIDs=Split(ids,",")

this to me seems alot better than a for each in loop
but is it bad skills? i am guessing it is, certainly if the elements have id= aswell as name=

just a thought.

thanks

nathan

mattglet

7:02 pm on Mar 11, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I personally split them into an array, but it's probably personal preference. You have to loop through them either way you choose...

I think it's a little more "clean" using an array, but you achieve the same results either way, with little if any performance loss/gain.

CaseyRyan

7:07 pm on Mar 11, 2005 (gmt 0)

10+ Year Member



Hi Natty,
I guess to answer your question would depend on what you're overall goal is, and how you go about writing your pages:

Your code would be a benefit in that from a reusability standpoint, you could slap it into the page and never have to do anything else. That's only if you're doing the same thing to every checkbox in every page you use it in. If in your section where you say "do something with the checkboxes", you're writing custom code to process each different checkbox then I would not use this method. It's considerably less efficient to loop through every item in the request collection than it is to access them directly.

I do like that you're using naming conventions for your form elements. I've seen systems (like survey/questionaire/polling) that follow convention for naming all of their form inputs and can thereby use generic code to process any of them into the database that they're storing them in. ie: checkbox would be named "chk_<questionID>_<answerID>".

anyway, that's my thoughts.

-=casey=-

natty

8:58 am on Mar 12, 2005 (gmt 0)

10+ Year Member



i think i was thrown by making my pages all xhtml strict and also giving everything an id as well as a name which i guess for form elements is mostly pointless. and as all the elements are generated dymaically making the name and id the same it would not validate.