[Webinar] Streamline your web hosting managementRegister Today


Creating an array of checked checkbox values

Posted on 2008-02-12
Medium Priority
Last Modified: 2013-12-24
I have a .cfm page that loops through a stored proc and populates the page with data returned from the query. One of the columns retuned by the query is TransID which is the the primary key of the table. Next to each TransID returned is a checkbox that the user can select. There are 2 forms on my .cfm page - "testArray" and "CorrectRecord". One is inside of the other. The outer form (testArray) is used to gather the TrasnID's of those records where the checkbox has been checked and direct me to another .cfm page where I dump the TransIDs of the array. The inner form is used to capture the TransID in that iteration of the loop and sends it to another .cfm page where I can look at the records in more detail.

Here is my code for this:

<CFSET TransIDs = ArrayNew(1)>
<CFSET i = ArrayLen(TransIDs) + 1>
<CFFORM action="testArray.cfm" method="POST" name ="testArray">

<CFLOOP query="GetRecords">

<td><div align="center" class="style6"><cfinput type="Checkbox" name="arrayValue" value="# TransID #"></div></td>

<CFSET TransIDs[i] = "#arrayValue#">
     CFFORM action="CorrectRecord.cfm" method="POST" name="CorrectRecord">
     <td><div align="center" class="style6"><input name="Submit" type="submit"class="style9"                  
     <cfinput type=hidden name="txtClaimControlNo" value="#TransactionID#">


<input type="Submit" name="Submit" value="Resubmit Checked Records">      

I need to gather the TransID's that the user has selected and store them in an array to pass on to a stored procedure in my database.
When I click on the Submit button for form "testArray", I get an error that #arrayValue# is undefined.
So I tried setting the array (within the loop) equal to the actual value of the TransID as follows:
<CFSET TransIDs[i] = "#TransID#">.
Now this actually takes me to the testArray.cfm page. However, I get a 4 digit number that makes no sense whatsoever when dumping the values. Furthermore, when I click the button inside of the inner form (CorrectRecord), I am still directed to testArray.cfm and I get the same weird number.
I know the machine is doing exactly what Im telling it to do, so can someone pease tell me what Im doing?????? Thanks!
Question by:ev72178
  • 3
  • 3
LVL 53

Expert Comment

ID: 20878268
> I get an error that #arrayValue# is undefined.

The field "arrayValue" is a checkbox.  In CF, checkboxes and radio buttons only exist IF they were checked. If they were not checked, you get an "undefined.." message.  Either check that the field exists using the IsDefined() function

<cfif IsDefined("form.arrayValue")>
     the field exists.. do something
    Sorry. the field does not exist

Or ensure it always exists by using cfparam.  It sets a default value if the field does not already exist

<cfparam name="form.arrayValue" default="">

But you do not really need (2) forms just to redirect a user to pageA.cfm or pageB.cfm depending on what button is clicked.  However, if you do use (2) cfforms I probably would not nest them.  I didn't even know that was allowed ;-)

Author Comment

ID: 20878455
1.) You are right - a "nested" form is not allowed. I will fix that later.
2.) I tried putiing <cfparam name = "form.arrayValue" Default=""> in my code and the page loads! However, all of the checkboxes are checked and I don't want this. Also, when I click the Resubmit Checked Records button, I get an error that my array variable is undefined.
LVL 53

Expert Comment

ID: 20878563
> a "nested" form is not allowed. I will fix that later.

     That may be related to your problem, so you might consider changing it now.

> all of the checkboxes are checked and I don't want this

    The code you posted does not mark the checkboxes as "checked", so I do not see how
    that could be happening.  Is it possible you omitted some code?

> I click the Resubmit Checked Records button, I get an error that my array variable is undefined.

    Is this a self-posting form (ie posted to the same page)? If not could you post the action page code
[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.


Author Comment

ID: 20878841
Sorry for the confusion. I have taken out my 2nd form among other changes. Here is my new code:

<CFFORM action="testArray.cfm" method="POST">
<cfparam name = "form.arrayValue" Default="">
<cfloop query="GetUnacceptedRecords">

<td><div align="center" class="style6"><cfinput type="Checkbox" name="arrayValue" value="#TransactionID#"></div></td>


<cfset TransIDs[i] = '#form.arrayValue#'>
<input type="Submit" name="Submit" value="Resubmit">

When I do a <cfdump var=#arrayValue#> on my action page, I get the values I have chosen in a comma delimited list. Rather than trying to create the array from the get go, should I do a list to array? And then I can pass the array to my stored procedure?
Assigning #arrayValue# to my array (TransIDs[i] ) gives me a TransIDs undefined error when I try to dump it.
LVL 53

Accepted Solution

_agx_ earned 2000 total points
ID: 20878941
> I get the values I have chosen in a comma delimited list. Rather than trying to
> create the array from the get go, should I do a list to array?

Yes. The reason is you cannot pass an array in a form field the way you're thinking.  

> Assigning #arrayValue# to my array (TransIDs[i] ) gives me a TransIDs undefined
> error when I try to dump it.

I'm not sure what you're trying to do with that part.  The cfparam shold be used on the  action page to avoid an error if none of the checkboxes were checked.  If you just trying to store all of the TransationID's in a hidden field, try using the ValueList(queryName.column) function.  It will create a comma separated list of all values that you can store in a hidden field.


Author Closing Comment

ID: 31430321
Thanks alot for your help, agx.
As far as my comment :
> Assigning #arrayValue# to my array (TransIDs[i] ) gives me a TransIDs >undefined error when I try to dump it.
I was just trying to further explain to you how I was not able to pass an array in a form field as I thougt!
Anyway...here is the code on my action page for anyone interested:

Hopefully other people will find this solution helpful.

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Periodically we have to update or add SSL certificates for customers. Depending upon your hosting plan you may be responsible for the installation and/or key generation. In the wake of Heartbleed many sites were forced to re-key. We will concen…
When it comes to showing a 404 error page to your visitors, you do not want that generic page to show, and you especially do not want your hosting provider’s ad error page to show either. In this article, I will show you how to enable the custom 40…
This video tutorial shows you the steps to go through to set up what I believe to be the best email app on the android platform to read Exchange mail.  Get the app on your phone: The first step is to make sure you have the Samsung Email app on your …
The video will let you know the exact process to import OST/PST files to the cloud based Office 365 mailboxes. Using Kernel Import PST to Office 365 tool, one can quickly import numerous OST/PST files to Office 365. Besides this, the tool also comes…

607 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question