ColdFusion HTML input array

Joined: 11/28/2008

Ok folks, I'm at a new job and am trying to learn ColdFusion. I've got a great handle on C#, C++, Java, PHP, ASP, but ColdFusion is a completely different animal. Ok, so on to the question I have for you CF experts!

Ok, so here’s the problem:

I normally set up my HTML form with input array elements like this:

CODE
<input name=”First_Name[]” size=”15” maxlength=”30” value=”Jason”><br>
<input name=”First_Name[]” size=”15” maxlength=”30” value=”Tommy”><br>
<input name=”First_Name[]” size=”15” maxlength=”30” value=”Henrik”><br>
<input name=”First_Name[]” size=”15” maxlength=”30” value=”Alexander”><br>

(note the [] I use on the end of the "name" attribute)

When I submit the form, the "action" page of my HTML form will look like this (if its in PHP):

CODE
<?
echo $_POST[“First_Name[]”]);
?>

The resulting output would look like this:

QUOTE
Jason,Tommy,Henrik,Alexander

Now, let's say that one of those HTML input array fields was left blank on the input side. Here's what it would look like in the HTML code:

CODE
<input name=”First_Name[]” size=”15” maxlength=”30” value=”Jason”><br>
<input name=”First_Name[]” size=”15” maxlength=”30” value=””><br>
<input name=”First_Name[]” size=”15” maxlength=”30” value=”Henrik”><br>
<input name=”First_Name[]” size=”15” maxlength=”30” value=”Alexander”><br>

When I submit the form, the "action" page of my HTML form will look like this (if its in PHP):

CODE
<?
echo $_POST[“First_Name[]”]);
?>

The resulting output would look like this:

QUOTE
Jason,,Henrik,Alexander

So in ColdFusion, I try to do the same thing with the "action" page of my HTML form:

CODE
<cfoutput>
#form.First_Name[]#
</cfoutput>

The compiler freaks out on that one and says I'm using "Java syntax".

So then I decided to take off the [] brackets from the input fields, and just give them the exact same name as you can see here:

CODE
<input name=”First_Name” size=”15” maxlength=”30” value=”Jason”><br>
<input name=”First_Name” size=”15” maxlength=”30” value=””><br>
<input name=”First_Name” size=”15” maxlength=”30” value=”Henrik”><br>
<input name=”First_Name” size=”15” maxlength=”30” value=”Alexander”><br>

When I submit the form, the "action" page of my HTML form will look like this (if its in ColdFusion):

CODE
<cfoutput>
#form.First_Name#
</cfoutput>

And the output looks like this:

QUOTE
Jason,Henrik,Alexander

THIS IS NOT WHAT I WANT. I want there to be another empty comma in that string like this:

QUOTE
Jason,,Henrik,Alexander

What gives with ColdFusion? I've tried every Google search I could think of regarding ColdFusion input arrays and can't get any pointers. The [] brackets are ESSENTIAL in order to have the HTML form fields give you a null value (and a comma) when the user has not filled out that input form field.

I would greatly appreciate any help on this folks!

Justin

Joined: 11/28/2008
Welp, I am a fairly bold guy,

Welp, I am a fairly bold guy, so I went directly to the source, Ben Forta. (He's the author of the ColdFusion 7 book I'm reading through here at work in teaching myself how to learn CF)

He responded back to my personal email this morning (major props there!) with this answer:

QUOTE
Justin,

Form fields are simple values, not arrays. PHP may do you a favor of turning them into arrays, and CF does the same but turns them into lists. Which is why you can't refer to the field with array syntax, but you could use list syntax.

And yes, you are correct, the lists eliminate unused elements (don't ask why, it's a leftover from a decision a decade ago).

For what it's worth, the "recommended" solution (and this is a general HTML recommendation, not a CF one) is to not reuse field names. In fact, in newer languages, like MXML, that is now illegal. What many CFers do is name the fields FirstName_1 FirstName_2 and so on, that makes it easy to build these programmatically, and also makes it easy to loop through them in the action page.

Of course, you could then create a simple function that turned those into an array, if you are more comfortable with that syntax.

I hope this helps.

--- Ben

Well, it's not the easy solution I was looking for, but I can live with it.

Joined: 11/28/2008
Boo on that solution!

Boo on that solution!

Who can bring a charge to God's elect? It is God who justifies!

Joined: 11/28/2008
Yeah I agree.But I'm stuck

Yeah I agree.

But I'm stuck with the way ColdFusion handles FORM submitted input arrays (because of a bad design decision many years ago) and have to work around it. I've got a working example where I number the fields like this: First_Name_1, First_Name_2, etc.

Just wish the array thing would be possible, as it would have made my life very easy.