Copy link to clipboard
Copied
I am runing a form that pulls the checkboxes from a query. They have the same name, but different values.
On the action page, for each checkbox checked, a new record must be created in the db.
Im' sure this has to do with counting and setting a loop number, but for some reason I am drawing a blank.
The query is basic....
<cfquery name="whatever" datasource=#dbname#>
insert into ach_world_done
(id,scoutid,den,world)
Values
('#url.id#','#url.scoutid#','#url.den#','#form.world#')
</cfquery>
As it is written now, it conmas the checkboxes on the previous page, so if there are two boxes checked, it does info1,info2 and puts this into the db. I need to break this up.
any help would be greatly appreciated.
Sharon
On the form page, give each checkbox a unique name:
<cfset index = 1>
<cfif doh.recordcount is 0>
<input name="belt#index#" type="checkbox" value="#belt.belttotemrecord#">
<cfelse>
<font class="normal">X - #dateformat(doh.datereceived,'mm/dd/yy')#</font>
</cfif></td>
<cfset index = index + 1>
On the action page, count the number of checkboxes. Remember Coldfusion doesn't submit unchecked checkboxes.
<cfset numberOfCheckboxes = 0>
<cfloop collection="#form#"
Copy link to clipboard
Copied
If your form has checkboxes, and they all have the same name, on your action page, you will get one of two things. If you boxes were checked, the variable won't exist. Otherwise, you will get a list.
You can loop throught a list.
Copy link to clipboard
Copied
In the form:
Use distinct names for the checkboxes, for example <cfinput type="checkbox" name="chkbx1">, <cfinput type="checkbox" name="chkbx2">, and so on.
In the action page:
As Dan has said, Coldfusion wont submit an unchecked checkbox. So, do a cfparam for all the checkbox fields, for example
<cfparam name="form.chkbx1" default="0">
<cfparam name="form.chkbx2" default="0">
and so on.
Copy link to clipboard
Copied
Thanks for the help. The checkboxes on the form are dynamic
<td>
<cfif #doh.recordcount# is 0>
<input name="belt" type=checkbox value="#belt.belttotemrecord#">
<cfelse>
<font class="normal">X - #dateformat(doh.datereceived,'mm/dd/yy')#</font>
</cfif></td>
The action page I need to loop through but not sure how to do it. How do I pass through a variable to set a loop? I think if I know how many boxes have been checked I can then set a loop variable, no?
Sharon
Copy link to clipboard
Copied
On the form page, give each checkbox a unique name:
<cfset index = 1>
<cfif doh.recordcount is 0>
<input name="belt#index#" type="checkbox" value="#belt.belttotemrecord#">
<cfelse>
<font class="normal">X - #dateformat(doh.datereceived,'mm/dd/yy')#</font>
</cfif></td>
<cfset index = index + 1>
On the action page, count the number of checkboxes. Remember Coldfusion doesn't submit unchecked checkboxes.
<cfset numberOfCheckboxes = 0>
<cfloop collection="#form#" item="field">
<cfif left(field,4) is 'belt'>
<cfset numberOfCheckboxes = numberOfCheckboxes + 1>
</cfif>
</cfloop>
Copy link to clipboard
Copied
This is beautiful...last stupid question....what an i inputting into the db now. The name is now dynamic...its changing...so I can no longer simply use
#form.belt# in my query...
Copy link to clipboard
Copied
You could do it within the same loop, like this
<cfset numberOfCheckboxes = 0>
<cfloop collection="#form#" item="field">
<cfif left(field,4) is 'belt'>
<cfset numberOfCheckboxes = numberOfCheckboxes + 1>
<cfset nameOfField = field>
<!--- output test --->
<cfoutput>#nameOfField#</cfoutput><br>
</cfif>
</cfloop>
Copy link to clipboard
Copied
BKBK is showing you the hard way. If you want to use that method, at the end of the day, you use array notation or another string function to get the value of the form field.
<cfloop collection="form" item="field">
<cfif left(field, 4) is "belt">
YourValue will either be form[field] or right(field, len(field) - 4) and what you do with that value is up to you.
If you go back to your original idea of giving all the checkboxes the same name with different values, your code becomes simpler. First, you don't need any cfparam tags. You just do this.
<cfif StructKeyExists(form, "belt">
<cfloop list = "#form.belt#" index = "field">
Your value is then field and what you do with it is up to you.
Copy link to clipboard
Copied
That completley fixed it and its working beautifully..I cannot thank you enough and to all that offered assistance!!!!