Skip to main content
Inspiring
July 19, 2007
Answered

Loop thru form variables

  • July 19, 2007
  • 9 replies
  • 3040 views
Hello,

I am posting a HUGE form to an intermediate page prior to action. Is there an easy way to loop thru these form.variables and re-declare?

Matt
    This topic has been closed for replies.
    Correct answer Newsgroup_User
    And when one changes a variable name, change all of them.

    <cfoutput>
    <cfloop list="#form.fieldnames#" index="field">
    <input type="text" name="#field# value="#form[field]#" />
    </cfloop>
    </cfoutput>

    9 replies

    Newsgroup_UserCorrect answer
    Inspiring
    July 20, 2007
    And when one changes a variable name, change all of them.

    <cfoutput>
    <cfloop list="#form.fieldnames#" index="field">
    <input type="text" name="#field# value="#form[field]#" />
    </cfloop>
    </cfoutput>
    Inspiring
    July 20, 2007
    fyrehed wrote:
    > Thanks all for your help, this might be the longer route, but it worked.
    >
    > <form method="post" action="fomefile.cfm">
    > <cfloop index="x" list="#form.fieldnames#">
    > <cfoutput>
    > <cfset name=#x#>
    > <cfset value=#form#>
    > <input type="text" name="#name#" value="#value#" />
    > </cfoutput>
    > </cfloop>
    > <input type="submit" value="Yes"></form>yes <FORM>
    >

    I would simplify the loop, I just dislike extraneous lines of code and
    try to avoid it whenever possible.

    <cfoutput>
    <cfloop list="#form.fieldnames#" index="field">
    <input type="text" name="#x# value="#form#" />
    </cfloop>
    </cfoutput>
    fyrehedAuthor
    Inspiring
    July 20, 2007
    Thanks all for your help, this might be the longer route, but it worked.

    <form method="post" action="fomefile.cfm">
    <cfloop index="x" list="#form.fieldnames#">
    <cfoutput>
    <cfset name=#x#>
    <cfset value=#form#>
    <input type="text" name="#name#" value="#value#" />
    </cfoutput>
    </cfloop>
    <input type="submit" value="Yes"></form>yes <FORM>
    Inspiring
    July 20, 2007
    fyrehed wrote:
    > Yes it is a huge form, with data entered from a user into a form and posted to
    > an intermediate page, then needs to be posted to the action page. Therefore, I
    > would have to hand code all the session variable sets. So that really wouldnt
    > be saving me much. I would like to find a way to set them with a loop if
    > possible, wether the outcome would be session or form variables, the problem
    > still remains.
    >

    If you really want a loop.

    <cfset session.theForm = structNew()>

    <cfloop collection="#form#" item="i">
    <cfset session.theForm = form>
    </cfloop>

    The one line version:
    <cfset session.theForm = duplicate(Form)>

    fyrehedAuthor
    Inspiring
    July 20, 2007
    Yes it is a huge form, with data entered from a user into a form and posted to an intermediate page, then needs to be posted to the action page. Therefore, I would have to hand code all the session variable sets. So that really wouldnt be saving me much. I would like to find a way to set them with a loop if possible, wether the outcome would be session or form variables, the problem still remains.
    Participating Frequently
    July 20, 2007
    Dan,

    Just a query.

    Is it advisable to store all the form field values in session object, as Matt has mentioned that this is a huge form? And the form must be containing huge number of form fields.

    Thanks
    fyrehedAuthor
    Inspiring
    July 19, 2007
    Nope...not the answer. Anyone see what i might do to try and cfset all these form.variables in a loop??
    Inspiring
    July 20, 2007
    What do you want to do want to do with the fields? If you're trying to output them, you don't need a cfset.

    <cfloop index="x" list="#form.fieldnames#">
    <cfoutput>#form#</cfoutput>
    </cfloop>
    fyrehedAuthor
    Inspiring
    July 19, 2007
    I am hoping this will work. Gonna try...



    <cfloop index="x" list="#form.fieldnames#">
    <cfoutput>
    <cfset x=#form#>
    </cfoutput>
    </cfloop>
    Inspiring
    July 19, 2007
    When you say redeclare do you mean pass them as hidden fields?
    Inspiring
    July 20, 2007
    Next time just check cflib.org. Just about anything you need to do, they have a function for it
    http://www.cflib.org/udf.cfm?id=534&enable=1