views:

85

answers:

3

I have several values in a select box. Is it possible to get both the value and the text between the <option> tags when the form is submitted?

<option value="413">Highland </option>
<option value="414">Inverclyde </option>

Alternatively I suppose I have to store the names in a table or array for retrieval but would be much easier if I could just insert both in the table when the form is submitted.

+2  A: 

If you've only got a few value/text pairs in your select, then just store the value. If you need to output the text somewhere else other than the select, just write a if/elseif/else or case block to display the text.

If you've got quite a few value/text pairs then it would be best to create a lookup table in your database with these in. You can use this to generate your select and output the text from a stored value at a later date.

How do you tell if you've got too many value/text pairs? If writing the case block to display them results in a silly amount of tedious code. ;)

Stephen Moretti
thanks guys, I hoped there was some CF function to do this. Would be nice but never mind.
Roscoeh
+4  A: 

As Stephen Moretti pointed out, there are at least two ways to derive the text from the value.

You could also use a list containing the value proper and the text for the value of the select. So, instead of:

<option value="23">Twenty Three</option>

use

<option value="23,TwentyThree">Twenty Three</option>

and use list*() functions on the back end.

Finally, you could use JavaScript to store the text of the selected option in a hidden field (or similar). This, in my opinion, is the least attractive option. First, it would be more work than the other options, and second because it will fail if JS is turned off on the client.

Depending on the size/type of data, I would probably either rewrite the option values, as I've described, or switch off a lookup table, as Stephen described.

Ben Doom
A: 

Another option would be to store a struct of your value/text pairs in the session scope, then on your form action page you can use the value to easily look up your text.

The beauty of this is that it's entirely server-side and does not include an extra trip to the database.

Delete the struct from the session scope if you don't want to keep it around.

Al Everett
good suggestion
Roscoeh