I don’t understand why ColdFusion insists on making things difficult. I’ve come to understand that it is not possible for ColdFusion to make use of inherent input arrays when one or more input elements is left empty. For some reason ColdFusion always drops the empty values from the array. I can’t think of any possible scenario where this is desired behavior. Given the following mark up:
<input type="text" id="first_widget" name="widget" value="First Widget" /> <!--- Notice the lack of a value in the following element ---> <input type="text" id="second_widget" name="widget" value="" /> <input type="text" id="third_widget" name="widget" value="Third Widget" />
Once submitted, parsing through the HTML array with .NET or PHP will produce:
widget[0] = “First Widget”
widget[1] = “”
widget[2] = “Third Widget”
With ColdFusion it ends up looking like:
widget[1] = “First Widget”
widget[2] = “Third Widget”
It completely drops the second array element because it’s an empty value. The obvious solution to this is to not have elements share a common name. I’ll index my form elements like so:
<input type="text" id="first_widget" name="widget_1" value="First Widget" /> <!--- Notice the lack of a value in the following element ---> <input type="text" id="second_widget" name="widget_2" value="" /> <input type="text" id="third_widget" name="widget_3" value="Third Widget" />
The argument could be made that indexing multiple common HTML elements is always a good idea, but I don’t like having to do it just to make up for the seemingly short coming of a program language. It also makes some additional work if I’m generating form elements dynamically and don’t know how many I’m going to end up with before the form is actually submitted.
Given the above mark up, I’m left to loop through each item to build my array, or structure, or error checking, etc. Whatever it is I want to do with the actual data, I’m left with an extra step to actually get it because of this.
Unless I’m really just missing something all together? Anybody know of a better way?











September 25, 2009 at 1:01 pm
I don’t know of a way, but it seems to me that CF handles it correctly. If you are naming your fields all the same, then you dont care which holds what data, what order they are in, etc. If do do care, then they should have different names.
Just my $0.02
September 28, 2009 at 7:26 am
Thanks for the comment Sean. What originally brought this about was me trying to be some-what lazy. I have a form with dynamically created elements, that once submitted, I would have to step through and count. If all of the form fields had a value, I could easily import the form array into a ColdFusion array and proceed with my logic on that manner. But because ColdFusion drops empty values, when I tried to dump the form array into my ColdFusion array the data was out of sync. I’ve since gone back and renamed all my form fields (which I know, technically is the best method) and added code to account for the dynamic names, I haven’t had an issue.