Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
525 views
in Technique[技术] by (71.8m points)

coldfusion - Accessing variables of a dynamic form

I am creating a form with cfloop and need to access each variable individually when submitting the form. I need to use the selected entries of the form in a loop that will add my selections to a database.

This is my form:

<form method="post">     
   <input type="hidden" name="isPost" value="1">
   ...
   <cfoutput>
   <cfloop query="client_admin_surveys">
      <input type="text" size="35" name="surveyID" id="surveyID" value="#id#">
      <input type="text" size="35" name="surveyName" id="surveyName" value="#name#">
      <input type="checkbox" name="amplify" id="amplify">          
      <input type="checkbox" name="enchance" id="enchance">
      <input type="checkbox" name="pacify" id="pacify">
      <input type="checkbox" name="pacifyUrgent" id="pacifyUrgent">
   </cfloop>
   </cfoutput>
   ...
   <input type="submit" name="submit" value="Submit">
</form>

After posting the form, the results group all of my selections because I have the same "name" for my form elements. I tried adding an i count next to each name to make it different but then I got a bit confused about how to process the fields.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You started down the correct path when you added the counter - go back and add that, something like:

<input type="checkbox" name="amplify#client_admin_surveys.currentRow#" id="amplify">

Would work.

I also sometimes like to add a form field for the 'counter' on the processing page

<input type="hidden" name="counter" value="#client_admin_surveys.recordCount#" />

Then on the processing page, you can loop over the counter and access the form fields using bracket notation

<cfloop from="1" to="#form.counter#" indexd="i">
    <cfset thisAmplify = form["amplify" & i] />
    <cfset thisEnhance = form["enhance" & i] />
    <!---- more logic here --->
</cfloop>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...