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>
5
  • 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, 2013 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, 2013 at 13:52
  • Leigh - what technique would you use?
    – Mike
    Oct 30, 2013 at 14:00
  • Dan - thank you. I dumped it and it only dumped the form fieldnames, without the text input.
    – Mike
    Oct 30, 2013 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, 2013 at 15:26

1 Answer 1

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.

11
  • 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, 2013 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, 2013 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. Oct 30, 2013 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, 2013 at 14:28
  • @Leigh: there's a challenge. I'll check on CF5 this evening... ;-) Oct 30, 2013 at 16:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.