Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Problem with multiple identical fields in form

Explorer ,
May 30, 2008 May 30, 2008
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
303
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 30, 2008 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.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 30, 2008 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.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 30, 2008 May 30, 2008
LATEST
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>
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources