1

In my Application.cfc file I set this.sameFormFieldsAsArray = True.

When I dump the form I see the values in the array. How do I access them now if I want to loop through them and do other actions?

UPDATE: I am trying to match up the values of two form fields that are being passed. If form.sched contains form.bldgarea, output the values of both. It is erroring out stating it can't find anything at position 4. Any ideas what I am doing wrong?

<cfloop index="i" from="1" to="#arrayLen(form.bldgarea)#">

   <cfloop index="i" from="1" to="#arrayLen(form.sched)#">

      <cfif #form.sched[i]# contains #form.bldgarea[i]#> 
        <cfoutput>
            #form.sched[i]#, #form.bldgarea[i]#          
         </cfoutput>
      </cfif>

    </cfloop>

</cfloop>
6
  • Hint: See the cfloop: looping over a list, a file, or an array documentation. Commented Mar 2, 2017 at 19:31
  • They two fields do not have the same number of values/elements. What type of form fields are they: text, checkbox, ...? Commented Mar 2, 2017 at 20:30
  • One is a checkbox with an output of a query as the value. The other is a dopdown select box . So if all goes as planned here is what I am trying to achieve: 91_Hall1, hall1 Commented Mar 2, 2017 at 20:38
  • (Edit) Hmm... so the ultimate goal is to extract the selected sched value only when the associated bldgarea box is checked? Does the query contain some sort of unique identifier column, like an ID? If yes, I would suggest a different form field structure instead. BTW, checkbox values are only submitted IF the box was checked. That is why the two field arrays have a different number of elements. Commented Mar 2, 2017 at 20:50
  • Yes the sched value is two fields concatonated. The sched id number which is a two digit number then an underscore, then the building name. So the sched ID and building name together are the key field. But since I am outputting a list of buildings and dropdown select boxes which results in tons of scheds getting submitted at the same time I am trying to figure out how to just get the first two digit sched numbers for the buildings in the form.bldgarea array. Commented Mar 2, 2017 at 20:52

1 Answer 1

0

Update:

If you are getting an error on submit, I suspect you are running into the security feature added to CF9+ which Limits the maximum number of POST fields allowed. A simple option is to increase the limit in the CF Administrator.

Another possibility is to restructure the form. Move the select lists into a separate <form>, so they are not submitted. When the user clicks the submit button in the main form, use jQuery to build a list of the selected items and store it in a hidden field. Similar to the approach in this thread. Then on the action page, loop through that received list of values.


To summarize the discussion in the comments:

The ultimate goal is to extract the selected sched value only when the associated bldgarea box is checked.

I would suggest a slightly different approach. Assuming you have some sort of (unique) numeric ID to represent each building, use that as the checkbox "value", rather than building name.

<input type="checkbox" name="bldgarea" value="#queryName.buildingID#">

Then use the building ID value to generate unique names for each of the select lists:

<select name="sched_#queryName.buildingID#">

When the form is submitted, form.bldgarea will only contain the id's of the selected buildings. Loop through that array and grab the associated sched value dynamically, using associative array notation.

  <cfloop array="#form.bldgarea#" index="variables.buildingID">

     <cfoutput>
        buildingID = #variables.buildingID#
        schedID = #FORM["sched_"& variables.buildingID]#
        <br>
     </cfoutput>
  </cfloop>

NB: Checkboxes are only submitted if something was checked. Be sure to verify form.bldgarea exists before using it.

Sign up to request clarification or add additional context in comments.

12 Comments

Thanks for this. I am playing around with it now. Should I be specifiying variables.buildingID or should it be something else like the form field?
Yes, variables is more appropriate here since it is a local page variable, not something submitted via POST. (Technically you could copy the id's into the form scope if needed, but there is no real benefit to doing that)
If I change the name of my select to sched_#bldgarea.id# I get a message that says bad request when I submit. Not sue what is going on.
The name alone should not cause an issue normally. Not sure without seeing the actual code you used and error message. Do you have any error handling enabled? If you are in Dev, temporarily disable it so you can see the real error. Also, check your log files.
(Edit) Hm... how many form fields total? Later versions of CF default to a maximum of 100 form fields as a security measure. The limit can be increased via the CF Admin for CF10+. That might be the issue.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.