Skip to main content
Inspiring
September 16, 2009
Answered

Dynamically checking off checkboxes

  • September 16, 2009
  • 2 replies
  • 1434 views

Hi,

I have a simple form with some checkboxes.  The user checks them off and the values get store in a db.

I like to eat:

Form page:

  <input type="Checkbox" name="fruit" value="apples">apples ....

- apples

- oranges

- bananas

- grapes

So on the edit page if the user has checked off all, but oranges it should be displayed from the db like so:

x apples

- oranges

x bananas

x grapes

I started by doing this, but it doesn't quit cut it.  Maybe merging the values of the form fields and the db fields into one list would work?

<cfoutput query="getinfo">
         <cfif fruit EQ '#fruit#'>

         <input type="Checkbox" name="fruit" value="#fruit#" checked>x apples

         <cfelse>

         <input type="Checkbox" name="fruit" value="#fruit#" >apples

</cfoutput>

Any ideas would be much appreciated!

This topic has been closed for replies.
Correct answer BKBK

Database table: fruitChoice

userID apple orange banana grape
x147d1011
a065b0111
t992w1010

Query to fetch data from the database:

<cfquery name="getInfo" datasource="myDSN">

select apple,orange,banana,grape

from fruitChoice

where userID = '#session.userID#'

</cfquery>

Query to insert data into the database:

<!--- I use the submit field, not a checkbox, to verify whether the form has been submitted. If you don't check a checkbox, x, then the variable form.x wont exist when the form is submitted --->

<cfif isDefined("form.sbmt")>

<cfset isAppleChosen=0>
<cfset isOrangeChosen=0>
<cfset isBananaChosen=0>
<cfset isGrapeChosen=0>
<cfif isDefined("form.apples")>
    <cfset isAppleChosen=1>
</cfif>
<cfif isDefined("form.oranges")>
    <cfset isOrangeChosen=1>
</cfif>
<cfif isDefined("form.bananas")>
    <cfset isBananaChosen=1>
</cfif>
<cfif isDefined("form.grapes")>
    <cfset isGrapeChosen=1>
</cfif>

<cfquery name="saveInfo" datasource="myDSN">

insert into fruitChoice (userID,apple,orange,banana,grape)

values ('#session.userID#',#isAppleChosen#,#isOrangeChosen#,#isBananaChosen#,#isGrapeChosen#)

</cfquery>

</cfif>

And, finally, the form

<form>
    <p>
    <cfif getinfo.apple EQ 1>
        <input type="Checkbox" name="apples" checked>apples
    <cfelse>
        <input type="Checkbox" name="apples">apples
    </cfif>
    </p>
    <p>
    <cfif getinfo.orange EQ 1>
        <input type="Checkbox" name="oranges" checked>oranges
    <cfelse>
        <input type="Checkbox" name="oranges">oranges
    </cfif>
    </p>
    <p>
    <cfif getinfo.banana EQ 1>
        <input type="Checkbox" name="bananas" checked>bananas
    <cfelse>
        <input type="Checkbox" name="bananas">bananas
    </cfif>
    </p>
    <p>
    <cfif getinfo.grape EQ 1>
        <input type="Checkbox" name="grapes" checked>grapes
    <cfelse>
        <input type="Checkbox" name="grapes">grapes
    </cfif>
    </p>
    <p>
    <input type="submit" name="sbmt" value="send">
    </p>
</form>

2 replies

BKBK
Community Expert
BKBKCommunity ExpertCorrect answer
Community Expert
September 18, 2009

Database table: fruitChoice

userID apple orange banana grape
x147d1011
a065b0111
t992w1010

Query to fetch data from the database:

<cfquery name="getInfo" datasource="myDSN">

select apple,orange,banana,grape

from fruitChoice

where userID = '#session.userID#'

</cfquery>

Query to insert data into the database:

<!--- I use the submit field, not a checkbox, to verify whether the form has been submitted. If you don't check a checkbox, x, then the variable form.x wont exist when the form is submitted --->

<cfif isDefined("form.sbmt")>

<cfset isAppleChosen=0>
<cfset isOrangeChosen=0>
<cfset isBananaChosen=0>
<cfset isGrapeChosen=0>
<cfif isDefined("form.apples")>
    <cfset isAppleChosen=1>
</cfif>
<cfif isDefined("form.oranges")>
    <cfset isOrangeChosen=1>
</cfif>
<cfif isDefined("form.bananas")>
    <cfset isBananaChosen=1>
</cfif>
<cfif isDefined("form.grapes")>
    <cfset isGrapeChosen=1>
</cfif>

<cfquery name="saveInfo" datasource="myDSN">

insert into fruitChoice (userID,apple,orange,banana,grape)

values ('#session.userID#',#isAppleChosen#,#isOrangeChosen#,#isBananaChosen#,#isGrapeChosen#)

</cfquery>

</cfif>

And, finally, the form

<form>
    <p>
    <cfif getinfo.apple EQ 1>
        <input type="Checkbox" name="apples" checked>apples
    <cfelse>
        <input type="Checkbox" name="apples">apples
    </cfif>
    </p>
    <p>
    <cfif getinfo.orange EQ 1>
        <input type="Checkbox" name="oranges" checked>oranges
    <cfelse>
        <input type="Checkbox" name="oranges">oranges
    </cfif>
    </p>
    <p>
    <cfif getinfo.banana EQ 1>
        <input type="Checkbox" name="bananas" checked>bananas
    <cfelse>
        <input type="Checkbox" name="bananas">bananas
    </cfif>
    </p>
    <p>
    <cfif getinfo.grape EQ 1>
        <input type="Checkbox" name="grapes" checked>grapes
    <cfelse>
        <input type="Checkbox" name="grapes">grapes
    </cfif>
    </p>
    <p>
    <input type="submit" name="sbmt" value="send">
    </p>
</form>

jenn1Author
Inspiring
September 21, 2009

Thank you so much for taking your time. It worked perfectly!

BKBK
Community Expert
Community Expert
September 21, 2009

!

Inspiring
September 16, 2009

Hi, I'm assuming that your db field looks like "apples, bananas, grapes"

I would do it like this

checked>apples checked> oranges checked> bananas checked>]]> grapes

Steve