Solved

CheckBoxes: numbered of boxes vary; transmit data for only the ones that are checked

Posted on 2004-10-05
20
196 Views
Last Modified: 2013-12-24
I have a query that generates a list of account numbers (the quantity per customer varies).  So, in <cfform tag, I use a <cfloop query to list the account numbers.  Next to each account number, I put an <cfinput type="checkbox" name="uniquename"> to let the customer decide whether to include that account in the service.

I want to transmit the following in a form:
if uniquenamea is checked then <cfset account1=#uniquenamea#>
otherwise, got to uniquenameb and <cfset account1=#uniquenameb#>
....
then do account2...account3.... until accountn  (which varies)

I believe it needs javascript, but I can't set it up to work.  Can you please help?



0
Comment
Question by:carolynusinger
  • 8
  • 4
  • 3
  • +1
20 Comments
 
LVL 35

Expert Comment

by:mrichmon
ID: 12229991
Basically you need to have a hidden input in the form

<input type="hidden" name="accountCount" value="#accountquery.RecordCount#">

Which is basically N  (so you know how many checkboxes there were.

Then in the page the form submits to:

<cfloop index="i" from="1" to="#form.accountCount#">
<cfif IsDefined("Form.Account#i#")>
 set variable or do wahtever
</cfif>
</cfloop>
0
 

Author Comment

by:carolynusinger
ID: 12230501
That's fine for the first loop, but then I have to assign Account2 - only looping through the accounts that weren't chosen during the first loop.
(I'm getting loopy)....
Example:   these accounts are listed:  apple, banana, cherry, dogfood
banana and dogfood are checked.

This is what I want to put into the database:
account1=banana
account2=dogfood

I can see doing the first loop to get banana...but I need to run it through again, beginning after banana to pick up dogfood for account 2.

Carolyn
0
 
LVL 35

Accepted Solution

by:
mrichmon earned 250 total points
ID: 12231124
Okay  not a problem...

SOmthing like this :

<cfset checkboxlist="apple,banana,cherry,dogfood">
<cfset accountnum = 0>

<cfloop index="currentbox" list="checkboxlist">
  <cfif IsDefined("Form.#currentbox#")>
        <cfset accountnum = accountnum + 1>
       <cfset Variables["account" & accountnum] = currentbox>
    </cfif>
</cfloop>

Once done you will know the number of accounts that were set by using accountnum and you can see the value that were set using :

<cfoutput>
Number of accounts set : #accountnum#<br>
<cfloop index="i" from="1" to="#accountnum#">
Account #i# : #Variables["account" & accountnum]#<br>
</cfloop>
</cfoutput>
0
 
LVL 1

Expert Comment

by:dcrysler
ID: 12231126
mrichmon's answer still works,  you need to create the names for the checkboxes as a combination of account number and item number or account number index and item number index.  Since checkboxes are only posted back to the server if they are checked  you need to re-create the checkbox name and see if a form variable with that name exists.

The following example assumes checkbox names of:

Account1Item1
Account1Item2
Account2Item1
etc...

<cfloop index="i" from="1" to="#ListLen(form.account_id)#">
  <cfloop index="j" from="1" to="#form.itemcount#">
    <cfif IsDefined("Account#i#Item#j#") is true>
        <!--- Do whatever --->
    </cfif>
  </cfloop>
</cfloop>
0
 

Author Comment

by:carolynusinger
ID: 12231695
OK...I'm dumb...but I did say that this was a 500 point question.

Here is the code on the first page:
<cfform name="accounts" method="post" action="accounts2.cfm">
<input type="checkbox" name="account1" value="220"><font size=2>Account 220
<br><input type="checkbox" name="account2" value="1240">Account 1240
<input type="checkbox" name="account3" value="024">Account 024  
<br><input type="checkbox" name="account4" value="163">Account 163
<br><input type="checkbox" name="account5" value="100">Account 100</td></tr></font>
<input type="hidden" name="accountCount" value=5>
<cfset checkboxlist="account1,account2,account3,account4,account5">
<input type="submit" value="submit" name="submit">
</cfform>
.......
next page (accounts2.cfm)

<cfset accountnum = 0>
<cfloop index="currentbox" list="checkboxlist">
  <cfif IsDefined("Form.#currentbox#")>
        <cfset accountnum = accountnum + 1>
       <cfset Variables["account" & accountnum] = currentbox>
    </cfif>
</cfloop>

<cfoutput>
Number of accounts set : #accountnum#<br>
<cfloop index="i" from="1" to="#accountnum#">
Account #i# : #Variables["account" & accountnum]#<br>
</cfloop>
</cfoutput>

When I run it, it says that there are 0 accounts set, regardless of how many I checked on the first page.  What I want  in the end is
(if I've checked the second and fourth boxes)
account_number1=1240
account_number2=163

Carolyn
0
 
LVL 35

Expert Comment

by:mrichmon
ID: 12231853
You didn't set the checkboxlist on the action page
<cfset checkboxlist = "account1,account2,account3,account4,account5">

But remember - checkboxes DO NOT return a value even if you set one - they are either checked or not.

So if you want the account numbers like that then you have two choices - either add that info as a hidden field or do something like knowing the order of the info.

If you set a hidden file :

      
Comment from mrichmon
Date: 10/05/2004 12:03PM PDT
ID: 12229991
      Your Comment       

Basically you need to have a hidden input in the form

<input type="hidden" name="accountCount" value="#accountquery.RecordCount#">

Which is basically N  (so you know how many checkboxes there were.

Then in the page the form submits to:

<cfloop index="i" from="1" to="#form.accountCount#">
<cfif IsDefined("Form.Account#i#")>
 set variable or do wahtever
</cfif>
</cfloop>

Comment from carolynusinger
Date: 10/05/2004 12:47PM PDT
ID: 12230501
      Author Comment       

That's fine for the first loop, but then I have to assign Account2 - only looping through the accounts that weren't chosen during the first loop.
(I'm getting loopy)....
Example:   these accounts are listed:  apple, banana, cherry, dogfood
banana and dogfood are checked.

This is what I want to put into the database:
account1=banana
account2=dogfood

I can see doing the first loop to get banana...but I need to run it through again, beginning after banana to pick up dogfood for account 2.

Carolyn

Comment from mrichmon
Date: 10/05/2004 01:40PM PDT
ID: 12231124
      Your Comment       

Okay  not a problem...

SOmthing like this :

<cfset checkboxlist="apple,banana,cherry,dogfood">
<cfset accountnum = 0>

<cfloop index="currentbox" list="checkboxlist">
  <cfif IsDefined("Form.#currentbox#")>
        <cfset accountnum = accountnum + 1>
       <cfset Variables["account" & accountnum] = currentbox>
    </cfif>
</cfloop>

Once done you will know the number of accounts that were set by using accountnum and you can see the value that were set using :

<cfoutput>
Number of accounts set : #accountnum#<br>
<cfloop index="i" from="1" to="#accountnum#">
Account #i# : #Variables["account" & accountnum]#<br>
</cfloop>
</cfoutput>

Comment from dcrysler
Date: 10/05/2004 01:40PM PDT
ID: 12231126
      Comment       

mrichmon's answer still works,  you need to create the names for the checkboxes as a combination of account number and item number or account number index and item number index.  Since checkboxes are only posted back to the server if they are checked  you need to re-create the checkbox name and see if a form variable with that name exists.

The following example assumes checkbox names of:

Account1Item1
Account1Item2
Account2Item1
etc...

<cfloop index="i" from="1" to="#ListLen(form.account_id)#">
  <cfloop index="j" from="1" to="#form.itemcount#">
    <cfif IsDefined("Account#i#Item#j#") is true>
        <!--- Do whatever --->
    </cfif>
  </cfloop>
</cfloop>

Comment from carolynusinger
Date: 10/05/2004 02:47PM PDT
ID: 12231695
      Author Comment       

OK...I'm dumb...but I did say that this was a 500 point question.

Here is the code on the first page:
<cfform name="accounts" method="post" action="accounts2.cfm">
<input type="checkbox" name="account1" value="220"><font size=2>Account 220<input type="hidden" name="accountnum1" value="220">
<br><input type="checkbox" name="account2" value="1240">Account 1240<input type="hidden" name="accountnum2" value="1240">
<input type="checkbox" name="account3" value="024">Account 024  <input type="hidden" name="accountnum3" value="100">
<br><input type="checkbox" name="account4" value="163">Account 163<input type="hidden" name="accountnum4" value="163">
<br><input type="checkbox" name="account5">Account 100<input type="hidden" name="accountnum5" value="100">
</td></tr></font>
<input type="hidden" name="accountCount" value=5>
<input type="submit" value="submit" name="submit">
</cfform>

<cfset accountnum = 0>
<cfloop index="currentbox" list="checkboxlist">
  <cfif IsDefined("Form.#currentbox#")>
        <cfset accountnum = accountnum + 1>
       <cfset Variables["account" & accountnum] = Form["accountnum" & accountnum]>
    </cfif>
</cfloop>

<cfoutput>
Number of accounts set : #accountnum#<br>
<cfloop index="i" from="1" to="#accountnum#">
Account #i# : #Variables["account" & accountnum]#<br>
</cfloop>
</cfoutput>

0
 
LVL 35

Expert Comment

by:mrichmon
ID: 12231879
AAACK that post somehow got really messy and showed all the other responsese so far.

Here it is again with the response inside ========= just to make sure it is clear....

=====================================================
 You didn't set the checkboxlist on the action page
<cfset checkboxlist = "account1,account2,account3,account4,account5">

But remember - checkboxes DO NOT return a value even if you set one - they are either checked or not.

So if you want the account numbers like that then you have two choices - either add that info as a hidden field or do something like knowing the order of the info.

If you set a hidden file :

<cfform name="accounts" method="post" action="accounts2.cfm">
<input type="checkbox" name="account1" value="220"><font size=2>Account 220<input type="hidden" name="accountnum1" value="220">
<br><input type="checkbox" name="account2" value="1240">Account 1240<input type="hidden" name="accountnum2" value="1240">
<input type="checkbox" name="account3" value="024">Account 024  <input type="hidden" name="accountnum3" value="100">
<br><input type="checkbox" name="account4" value="163">Account 163<input type="hidden" name="accountnum4" value="163">
<br><input type="checkbox" name="account5">Account 100<input type="hidden" name="accountnum5" value="100">
</td></tr></font>
<input type="hidden" name="accountCount" value=5>
<input type="submit" value="submit" name="submit">
</cfform>

<cfset accountnum = 0>
<cfloop index="currentbox" list="checkboxlist">
  <cfif IsDefined("Form.#currentbox#")>
        <cfset accountnum = accountnum + 1>
       <cfset Variables["account" & accountnum] = Form["accountnum" & accountnum]>
    </cfif>
</cfloop>

<cfoutput>
Number of accounts set : #accountnum#<br>
<cfloop index="i" from="1" to="#accountnum#">
Account #i# : #Variables["account" & accountnum]#<br>
</cfloop>
</cfoutput>

=====================================================
0
 
LVL 1

Assisted Solution

by:dcrysler
dcrysler earned 250 total points
ID: 12232007
Ok, I misunderstood the problem.  Name all of the checkboxes with the same name.  If no checkboxes are checked then the form variable will not exist, otherwise the form variable will contain a comma seperated list containing the "value" of each checked checkbox.

On first page :

<input type="checkbox" name="Accounts" value="100" />
<input type="checkbox" name="Accounts" value="101" />
<input type="checkbox" name="Accounts" value="102" />
<input type="checkbox" name="Accounts" value="103" />

On second page :

<cfif IsDefined("FORM.Accounts") IS TRUE>
  <cfloop index="i" from="1" to="#ListLen(FORM.Accounts)#">
      Account #i# : #ListGetAt(FORM.Accounts,i)# <br>
  </cfloop>
</cfif>
0
Superior storage. Superior surveillance.

WD Purple drives are built for 24/7, always-on, high-definition security systems. With support for up to 8 hard drives and 32 cameras, WD Purple drives are optimized for surveillance.

 

Author Comment

by:carolynusinger
ID: 12232692
We're there!

Here is the first page:
<cfform name="accounts" method="post" action="secondpage.cfm">
<input type="checkbox" name="accounts" value="220">220
<input type="checkbox" name="accounts" value="1240">1240
<input type="checkbox" name="accounts" value="024"> 024
<input type="checkbox" name="accounts" value="163">163  
<input type="checkbox" name="accounts" value="100"> 100
<input type="submit" value="submit" name="submit">



Here is the second page:

<cfset #accounts#=#form.accounts#>
<cfset myArrayList = #ListToArray(form.accounts)#>
 <cfoutput>#ArrayLen(myArrayList)#
 </cfoutput> elements.
<cfset num=1>
<cfset #howmany#=0>
<cfloop index = "accounts"
      list = "#accounts#">
<cfif isdefined("accounts") is true>
<cfset "account#num#"=#accounts#>
<cfset #howmany#=#howmany#+1>
</cfif>
<cfset #num#=#num#+1>
</cfloop>

<cfset num=1>
<cfloop index="num" from="1" to="5">
<cfif isdefined("account#num#") is true>
<cfoutput>#evaluate("account#num#")# is this<br></cfoutput>
<cfset num=#num#+1>
</cfif>
</cfloop>

Depending on which boxes I check, I get:
4 elements. 1240 is this
024 is this
163 is this
100 is this

Thank you!!!
0
 
LVL 17

Expert Comment

by:Tacobell777
ID: 12233115
I might be missing something here, but doesn't #form.accounts# give a list of values that were selected?
0
 
LVL 35

Expert Comment

by:mrichmon
ID: 12238425
THe question started out

"Next to each account number, I put an <cfinput type="checkbox" name="uniquename"> to let the customer decide whether to include that account in the service."

Which means each checkbox had a unique name....
0
 
LVL 1

Expert Comment

by:dcrysler
ID: 12238692

True, but if the goal of the problem is to obtain a list of checked accounts then I believe using unique names and then having to recreate a list that would otherwise be created automatically by the http post is a waste of energy.

0
 
LVL 35

Expert Comment

by:mrichmon
ID: 12239333
Depends on the situation.

In many situations there are other needs for the uniquenames - such as if you do anything with javascript.

In this case though it probably would work - at lest for what we know about this question.
0
 
LVL 17

Expert Comment

by:Tacobell777
ID: 12241646
if you need to do anything different with javascript then you would assign a different id to each checkbox, so personally I think he needs to rename his checkboxes and give them all the same name, submit the form and voila, a list of values..
0
 
LVL 35

Expert Comment

by:mrichmon
ID: 12241728
Ah that depends on what you are doing via javascript and what browser....

There are some things that actually REQUIRE a name along with an ID at this point.  Hopefully this will eventually move to just ID, but well, for now there are some cases for name to be unique as well as ID.
0
 
LVL 1

Expert Comment

by:dcrysler
ID: 12242573

I do agree that there are some cases in which it makes sense to use unique names.  But I've always considered this to be the exception rather than the rule.  Using same names with unique ids gives you a lot of flexibility. So I've never understand why there would be a push to get rid of names.  Any enlightenment would be greatly appreciated.

For example, to sum up a bunch of line items on an order form I use the document.getElementsByName() method and loop through the resulting array.  Unless I'm missing something, the only real way to do this without the name is to maintain a list of ids and use them to get object references one at a time.  This seems kind of cumbersome.  Is there a better way than that?  Also, without names what happens to radio buttons?  Is there a better way to group them together without using a name?

0
 
LVL 35

Expert Comment

by:mrichmon
ID: 12243661
I am not saying it is the rule over the exception.

I am just saying there are cases to do it each way - in my experience it has been about equal number of times each way was most appropriate solution....
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

If you don't have the right permissions set for your WordPress location in IIS, you won't be able to perform automatic updates. Here's how to fix the problem.
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video discusses moving either the default database or any database to a new volume.

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now