5

I have the following form on the page form.html and it submits to cfpage.cfm. The first name, last name, address, and age all show up, but not in a consistent order. Sometimes it will show last name, first name, address, and age. In another instance it may show address, first name, age, and then last name.

How can I display the CFLoop items - with the text the user inputs in the text boxes - in the order they are shown in the form? I have multiple generic forms so I have to use a bit of generic code on cfpage.cfm to capture whatever the feeding form is submitting.

<form id="theform" name="theform" action="cfpage.cfm" method="post">
First Name
<input type="text" name="first name">

Last Name
<input type="text" name="last name">

 Address
<input type="text" name="address">

Age
<input type="text" name="age">
</form>

Code on cfpage.cfm

<cfloop collection="#form#" item="theField">
<cfif theField is not "fieldNames">
#theField# = #form[theField]#<br>
</cfif>
</cfloop>
Mike
  • 947
  • 2
  • 12
  • 18
  • 1
    Dump the form.fieldnames variable. If it consistently shows up in the same order as the fields are presented on the form, then Bob's your uncle. – Dan Bracuk Oct 30 '13 at 13:44
  • 2
    FYI, the reason is because structures, like `form`, do not have a guaranteed order. If you want a consistent order, you must use a different technique. I do not typically use `form.fieldnames`, but I suspect it returns the field names alphabetical order. – Leigh Oct 30 '13 at 13:52
  • Leigh - what technique would you use? – Mike Oct 30 '13 at 14:00
  • Dan - thank you. I dumped it and it only dumped the form fieldnames, without the text input. – Mike Oct 30 '13 at 14:01
  • That was what was supposed to happen. You then read the list and see if the fields are in the order you had hoped. In any event, this turned out to be the answer. – Dan Bracuk Oct 30 '13 at 15:26

1 Answers1

7

If you want them in the same order they appear on the form, then you must loop using this mechanism:

<cfloop index="i" list="#Form.FieldNames#" delimiters=",">
    #Form[i]#
</cfloop>

Here is code that validates the problem you are seeing, and that shows the loop above works -- save as stacktest.cfm:

<form id="theform" name="theform" action="stacktest.cfm" method="post">
First Name <input type="text" name="first name">
Last Name <input type="text" name="last name">
Address <input type="text" name="address">
Age <input type="text" name="age">
<input type="submit" value="submit"/>
</form>

<cfoutput>
<cfloop collection="#form#" item="theField">
<cfif theField is not "fieldNames">
    #theField# = #form[theField]#<br>
</cfif>
</cfloop>

<cfloop index="i" list="#Form.FieldNames#" delimiters=",">
    #i# = #Form[i]#<br>
</cfloop>
</cfoutput>

Update: Second loop provides same output as first loop now, only in order. Updated by request of user who asked question.

David Fleeman
  • 2,588
  • 14
  • 17
  • Thank you. The #Form[i]# dumps the text input answers in perfect order, but without the field names. How will I be able to have the first name, last name, address, and age field names show up next to the input text? – Mike Oct 30 '13 at 13:59
  • 1
    @Mike - Take a closer look at his example. He is looping through a list of field names, so the index `i` contains the field name. – Leigh Oct 30 '13 at 14:01
  • @Leigh, my recollection was the same as yours, but `fieldNames` *does* preserve the order in which they appeared on the form. I just verified this. – Adam Cameron Oct 30 '13 at 14:18
  • @AdamCameron - Thanks, I just got a chance to confirm it myself. I use it so infrequently, I guess I never checked. Huh.. I wonder how long it has been that way? :) – Leigh Oct 30 '13 at 14:28
  • @Leigh: there's a challenge. I'll check on CF5 this evening... ;-) – Adam Cameron Oct 30 '13 at 16:56
  • How long has it been that way? My guess is forever. – Dan Bracuk Oct 30 '13 at 17:18
  • @Leigh: confirmed: it's like that back to CF5. – Adam Cameron Oct 31 '13 at 06:57
  • @AdamCameron - No point checking CF4.5 then ;-) It is a pleasant surprise at least. Nice to see they did the right thing with form fields, unlike query columns. – Leigh Oct 31 '13 at 13:54
  • @Leigh: indeed. I even tried to suggest they fix that, and the reaction was "why? Anyway, it'd break backwards compat" :-/ – Adam Cameron Oct 31 '13 at 16:27
  • @AdamCameron - Funny... from the perspective of most developers it was broken to begin with. So why are we preserving that behavior again? ;-) – Leigh Oct 31 '13 at 19:33
  • @Leigh I have long since ceased trying to understand the rationale behind some... things. – Adam Cameron Oct 31 '13 at 20:03