Accessing variables of a dynamic form

人走茶凉 提交于 2019-12-29 09:32:16

问题


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.


回答1:


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>


来源:https://stackoverflow.com/questions/17951117/accessing-variables-of-a-dynamic-form

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!