Skip to main content
November 22, 2009
Answered

Quetion regarding checkboxes and inputting information

  • November 22, 2009
  • 2 replies
  • 11577 views

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

www.smdscouts.com

This topic has been closed for replies.
Correct answer BKBK

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>

2 replies

November 22, 2009

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

BKBK
BKBKCorrect answer
Community Expert
November 22, 2009

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>

Inspiring
December 1, 2009

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...

Inspiring
November 22, 2009

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.

BKBK
Community Expert
November 22, 2009

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.