Skip to main content
Known Participant
May 30, 2008
Question

Problem with multiple identical fields in form

  • May 30, 2008
  • 3 replies
  • 348 views
Hi

I'm having a problem with a form - the easiest way to describe it is like a shopping cart which gives you the product ID then a choice (radio buttons) of 3 colours and then a free text box.

My problem is where I have 4 of these rows. I can use a counter to rename the form fields (prodID#COUNTER#, col#COUNTER# =Red/Blu/Wht, description#COUNTER#) but then I'm struggling to deal with these in a loop on the action page.

Am I approaching this the right way in the form and what is the best method in the action?

Many thanks
Michael
    This topic has been closed for replies.

    3 replies

    Known Participant
    May 30, 2008
    Thanks Dan,

    That got me on the right track although I didn't really use your method!

    Below is my solution

    FormPage:

    <cfoutput>
    <cfset i=0>
    <cfloop query="cartList">
    <cfset i=#i#+1>
    <tr>
    <th align="left"><input type="hidden" name="prod#i#" value="#prodID#" />#prodID#</th>
    <td><input type="radio" name="colour#i#" value="Red" />Red</td>
    <td><input type="radio" name="colour#i#" value="Blu" />Blue</td>
    <td><input type="radio" name="colour#i#" value="Wht" />White
    <input type="text" name="txt#i#" size="50" maxlength="100" />
    <input type="hidden" name="txt#i#" value=",xxx," /></td>
    </tr>
    </cfloop>
    <input type="hidden" name="noItems" value="#i#" />
    </cfoutput>


    Action Page

    <cfloop from="1" to="#form.noItems#" step="1" index="i">
    Product: #form["prod" & i]#, colour: #form["colour" & i]#, Text: #form["txt" & i]#<br />
    </cfloop>
    May 30, 2008
    Instead of looping the entire product, why not just loop the colors associated to the product? Just have one product, then loop over the colors?

    If you're unsure which fieldnames are being defined on your action page, you can always loop through the "FIELDNAMES" list.
    Inspiring
    May 30, 2008
    You are approaching this the right way. The approach I generally use resembles:

    <cfloop list="#form.fieldnames#" index = "ThisFieldName">
    <cfif left(ThisFieldName is "prodId>
    do something
    etc

    Hopefully you know how to use array notation with form fields.