2 Replies - 3440 Views - Last Post: 14 August 2013 - 07:07 AM Rate Topic: -----

#1 midasxl  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 193
  • Joined: 03-December 08

Build Array using CFLOOP with Form Values

Posted 13 August 2013 - 06:15 AM

Hello and thanks for your time!

I can't quite get my head around this one.

I have four radio groups:

<input type="radio" name="DHCP0" value="yes" />
<input type="radio" name="DHCP0" value="no" />

<input type="radio" name="DHCP1" value="yes" />
<input type="radio" name="DHCP1" value="no" />

<input type="radio" name="DHCP2" value="yes" />
<input type="radio" name="DHCP2" value="no" />

<input type="radio" name="DHCP3" value="yes" />
<input type="radio" name="DHCP3" value="no" />



When the overall form is submitted, I would like to gather the values of these radio options and put them in an array.

<cfset dhcpArray = ArrayNew(1)>
<cfloop index="i" from="0" to=#dhcpCount#>
     <cfset ArrayAppend(dhcpArray, #FORM.DHCP##i#)>
</cfloop>



I'm geting hung up with the ArrayAppend() method. The way it is written above throws the error "FORM.DHCP is undefined". This makes sense to me, but I do not know any other way to write it without getting invalid CFML construct errors.

Any assistance would be appreciated. Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Build Array using CFLOOP with Form Values

#2 Craig328  Icon User is offline

  • I make this look good
  • member icon

Reputation: 1924
  • View blog
  • Posts: 3,462
  • Joined: 13-January 08

Re: Build Array using CFLOOP with Form Values

Posted 13 August 2013 - 08:27 AM

Hey midasxl! From what I understand of your issue, you're trying to refer to form elements whose names you may not know. I call that "dynamically named form elements" but that's me. There's a few ways you can handle this. One would be to set a CFPARAM tag at the top of the page for each form element you're wanting to refer to within your loop. However, that suggests you'll know what form elements to expect and if you know that you can do something similar within your CFLOOP. So, let's go with the other option.

You need to be able to test to see if you have a form element whose name you'll construct via your CFLOOP before you try and set its value into an array, right? For that you'll use either the IsDefined() or StructKeyExists() CF functions. By way of explanation, the StructKeyExists() function is valid here because CF treats the form scope (as well as other variable scopes) as a structure. That said, because CF treats the form scope as a structure, you can use structure syntax to get the variable. So, with that in mind, testing for the existence of your form variable would look something like this (first operation you'd do within your CFLOOP):
<cfif IsDefined("Form.DHCP#i#")>
OR
<cfif StructKeyExists(Form,"DHCP#i#")>

Either method is valid here. So, now that you see how you can do this using structure syntax, you can probably discern the rest on your own. Personally, I'd set an interim value as a placeholder for what I'd try and insert into the array and then insert the placeholder. Something like this:
<cfset dhcpArray = ArrayNew(1)>
<cfloop index="i" from="0" to=#dhcpCount#>
     <cfset variables.newArrayVal = "">
     <cfif IsDefined("Form.DHCP#i#")>
          <cfset variables.newArrayVal = Form["DHCP#i#"]>
          <cfset ArrayAppend(dhcpArray, variables.newArrayVal)>
     </cfif>
</cfloop>


Of course, you can entirely skip the step where you use the variables.newArrayVal altogether but referring to form variables via structure syntax can be a little confusing at first so it helps to break it down to a more mechanical look. I noticed that the form example you provided meant that a user could submit the form and have no value for your DHCP numbered variables...so unless you're okay with submitting blanks into your array, you could also check that the variables.newArrayVal actually possesses a value to add to the array...but again, that's up to you and depends on the logic of the rest of your process.

Hope that helps!

This post has been edited by Craig328: 13 August 2013 - 08:29 AM

Was This Post Helpful? 1
  • +
  • -

#3 midasxl  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 193
  • Joined: 03-December 08

Re: Build Array using CFLOOP with Form Values

Posted 14 August 2013 - 07:07 AM

This is a fantastic solution and description; thank you very much! It works great. I delve into ColdFusion markup on occasion due to some legacy apps. Now that I have a complete working solution (of this small portion of the overall form), here is this section of the form to possibly benefit others who may view this. Thanks!

The HTML begins with a div holding a pair of radio buttons. The user has the option of adding additional radio buttons via a jquery clone method. The div around the radio buttons is the tag that is cloned (including its contents). Upon being cloned the names of the radio buttons are changed to differentiate them from the first cloned pair.

HTML (before clone):

<div>
<input type="radio" name="DHCP0" value="yes" />  
<input type="radio" name="DHCP0" value="no" />  
<input type="hidden" name="dhcpCounter" value="dhcpCounter" />
</div>




HTML (after 1 clone):

<div>
<input type="radio" name="DHCP0" value="yes" />  
<input type="radio" name="DHCP0" value="no" />  
<input type="hidden" name="dhcpCounter" value="dhcpCounter" />
</div>

<div><!-- I am a clone of the above div, appended here -->
<input type="radio" name="DHCP1" value="yes" />  
<input type="radio" name="DHCP1" value="no" />  
<input type="hidden" name="dhcpCounter" value="dhcpCounter" />
</div>




I keep track of how many radio pairs using the hidden field "dhcpCounter"

CFML:
<cfset dhcpCount = listLen("#FORM.dhcpCounter#", ",")>



Then I build an array of the user selections from each radio pair. The form is validated on the front end which requires the user to make a selection; no empty values come across.

CMFL (cont'd):
<cfset dhcpCount = listLen("#FORM.dhcpCounter#", ",")>
<cfset dhcpArray = ArrayNew(1)>
<cfloop index="i" from="0" to=#dhcpCount#>
   <cfset variables.newArrayVal = "">
   <cfif isDefined("FORM.DHCP#i#")>
      <cfset variables.newArrayVal = FORM["DHCP#i#"]>
      <cfset ArrayAppend(dhcpArray, variables.newArrayVal)>
   </cfif>
</cfloop>



Once the array is built (and this could be up to 9 groups of radio buttons), I process the array into an html email:

CFML (cont'd):
<cfloop index="i" from="1" to="#dhcpCount#">
   <cfparam name="dhcpArray[i]" default="na"><!---if not already defined, define it here with the default value--->
   <cfif dhcpArray[i] is not "na">
      <p>Does host obtain IP through DHCP? #dhcpArray[i]#</p>
   </cfif>
</cfloop>



That's the bulk of it at this point. There's a whole lot more going on around this small portion of the form, but this tackles the issue I was having. Hope this helps others, and a BIG thanks to Craig328 for helping me!

Cheers!

This post has been edited by midasxl: 14 August 2013 - 07:09 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1