Link to home
Start Free TrialLog in
Avatar of ev72178
ev72178Flag for United States of America

asked on

Creating an array of checked checkbox values

Hello.
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:
<CFSTOREDPROC PROCEDURE="Records" DATASOURCE="SQLServer2005">
<CFPROCRESULT NAME="GetRecords">
</CFSTOREDPROC>

<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"                  
     value="#TransID#"></td>
     <cfinput type=hidden name="txtClaimControlNo" value="#TransactionID#">
     </CFFORM>

</CFLOOP>

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

Problem:
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!
Avatar of _agx_
_agx_
Flag of United States of America image

> 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
<cfelse>
    Sorry. the field does not exist
</cfif>

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 ;-)
Avatar of ev72178

ASKER

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.
> 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
Avatar of ev72178

ASKER

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>

</cfloop>

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

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.
                        
                        
ASKER CERTIFIED SOLUTION
Avatar of _agx_
_agx_
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ev72178

ASKER

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.