• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 289
  • Last Modified:

Passing dynamically named checkbox values to an appended list (spectra/CF)

I don't know how difficult this question is to answer, but it's worth a lot to me: 200 points!

I created about fifty dynamically named checkboxes by looping through a set of objects and naming each checkbox with the objectID in the structure of each object. (I'm actually using Spectra objects here, but essentially I think this would be the same as straight CF)

When I submit the form, I want to take the ObjectIDs which come from the checked checkboxes and use listAppend() to create a comma-delimited list. I'll then insert the list into a property of an object I'm trying to update.

My code breaks down when I try to make the list. After I submit the form, I believe I have to loop through the same set of objects to recreate the names of the checkboxes and see if they were checked when submitted.

I thought that normally with checkboxes, if the box is checked when submitted then form.formfield for that checkbox would evaluate as "on" and if it is empty when submitted, it would evaluate as "off". But if I try to say <cfif form.Product[ct].objectID eq "on"> then append the ID to the list, it never works because form.product[ct].objectid always evaluates to "form.74DB80CD-593B-11D5-9C440090277322BD" or whatever the ID of the current object is.

Here is my most recent rendition of my code (note that I get the content from a Spectra content-object database):

<!--- Get all the product Overviews to populate checkbox form items and make "productList" list--->
            dataSource = "#prgn_corp_db#"
               typeID = "#tpsProductOverview#"
               r_stObjects = "stProducts"

                struct = "#stProducts#"
                commonSubKey = "psSortOrder"
            sortType = "numeric"
                sortOrder = "asc"
                r_aSortedKeys = "productKey"

<cfif isdefined("form.ApplyChanges")>

<!--- Establish ObjectID to update from Hidden Form Field --->
      <cfset url.objectid ="#form.objectid#">

<!--- Create list of Product UUIDs from selected form checkbox items. --->

<cfset productList = "">
<cfloop list=#arraytolist(productKey)# index="ctX">
      <cfset productUUID = replace(stProducts[ctx].objectid, "-", "_", "all")>
<!--- This is the problem! --->
      <cfif isDefined(evaluate(de('form.' & productUUID)))>
      <cfset productList = listAppend(productList, stProducts[ctx].objectID)>
<!--- This is seemingly the end of my problem - my list is empty --->

<!--- update the content object property --->

        dataSource = "#prgn_corp_db#"
      objectID = "#form.objectid#"



<!--- Read the Staged Object to which you want to add the list of Product UUIDs --->
      datasource = "#prgn_corp_db#"
      objectid = "#url.objectid#"
      r_stObject = "stProfile"

<cfform name="Partnersform" action="pstPRGNPartnersEditOne.cfm?myaction=#URLEncodedFormat(myAction)#&typeid=#URLEncodedFormat(url.typeid)#" method="post">
<input type="hidden"  name="query_string" value="#cgi.query_string#">
<input type="hidden"  name="objectid" value="#url.objectid#">      

<!--- display checkboxes of all possible products that stProfile can be associated with --->
<table border="0" cellpadding="3" cellspacing="4" width="99%">

      <cfloop list="#arraytolist(productKey)#" index="ct">

            <!--- replace all ID hyphens with underscores --->
          <cfset productUUID = replace(stProducts[ct].objectID, "-", "_", "all")>
            <cfif structKeyExists(stProfile.psAssociatedProducts, "#mylanguage#") and stProfile.psAssociatedProducts[mylanguage] contains "#stProducts[ct].objectID#">
            <cfinput type="Checkbox" name="#productUUID#" checked>
            <cfinput type="Checkbox" name="#productUUID#">
      <font size="-3">#stProducts[ct].psProductTitle#</font>

<input type="submit" NAME="ApplyChanges" value="Stage">  

1 Solution
The problem is with your checkboxes.

Each checkbox has to have the same name, but you set the value as the productuuid, you then get all the ticked checkboxes upon submit (all unchecked ones are not passed) as a comma delimited list of the uuid values you want.

This small example shows a working example.

<cfif isdefined("form.submited")>
     value = <cfoutput>#form.test#</cfoutput>

<form action="index.cfm" method="post">
     <input type="checkbox" name="test" value="123">
     <input type="checkbox" name="test" value="456">
     <input type="checkbox" name="test" value="789">
     <input type=submit name="submited" value="go">
KateDeJongAuthor Commented:
Thanks for your help! I knew radio buttons needed the same name, but I thought checkboxes were separate entities!

I spent a long time trying to come up with a "complex" CF solution, and now it's been fixed in a matter of minutes.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now