[thelist] Friday Freebie

Scott Dexter sgd at ti3.com
Fri Jan 12 13:25:34 CST 2001


(sorry for the inconsistency lately. Things here real fricken busy)

two for one special today

<tip type="asp" subject="getting the most outta yer forms" author="sgd">
Seems almost trivial at this point. User populates form, server side script
reads from database and populates a page, filling in form values where
appropriate. Rinse, repeat.

In cases where you have a form with lots of information to track, you can
'group' information pseudo-magically with a couple tricks:

1) leverage javascript's auto-array feature if more than one input is named
the same (its not just for radio buttons anymore!)
2) group items by variable name, parsing the information into neat little
boxes on the server side

(1) Leveraging javascript
	Because of the way the DOM works, everything is an array. The
obvious examples are radio buttons and checkboxes. But you can also do:

Address: <input type="text" name="address">
City: <input type="text" name="address">
State: <input type="text" name="address">
Zip: <input type="text" name="address">

and the DOM will be more than happy to create a form.address array, and when
the form is submitted, you get a comma delimited list for the address
variable. Store as is, or parse it and store as you like.

(2) Grouped variable names
	Not as elegant as the above, but something like:

Address: <input type="text" name="address_street">
City: <input type="text" name="address_city">
State: <input type="text" name="address_state">
Zip: <input type="text" name="address_zip">

would help some; on the server side you scan your form variables and handle
them with a Case statement (looking for the prefix you used). But where this
method can come in handy is when you're creating the form from server side
code, and need a way to accurately name the fields:

Joe <input type="text" name="Joe_address">
Mary <input type="text" name="Mary_address">
Steve <input type="text" name="Steve_address">

Now when you process the form, you can correctly update the information for
each/any of the members.

(and yeah, combining the two would help as well)

Is this a performance boost? Not really. Its a method to modularize your
information without going crazy with client side DOM objects and/or XML. And
it makes your code on the server side workable, possibly a little more
compact, and the big bonus: its nicer to maintain/tweak.

</tip>

<tip type="javascript" subject="caveat with variable names">
As the above technique is a great way to move more information around inside
the variable names, keep in mind the only safe delimiter you can use is the
underscore ("_"). Also you can't begin a variable name with a number.

Oh, and did you remember that javascript is also case sensitive?
</tip>

sgd
--
work: http://www.ti3.com/
non: http://thinksafely.org/




More information about the thelist mailing list