Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Dynamically checking off checkboxes

Participant ,
Sep 16, 2009 Sep 16, 2009

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!

TOPICS
Getting started
1.3K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Sep 18, 2009 Sep 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 isD

...
Translate
Explorer ,
Sep 16, 2009 Sep 16, 2009

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

I would do it like this

<input type="Checkbox" name="fruit" value="apples#" <cfif listfind(fruit,"apples") eq 1>checked</cfif>>apples <input type="Checkbox" name="fruit" value="oranges" <cfif listfind(fruit,"oranges") eq 1>checked</cfif>> oranges <input type="Checkbox" name="fruit" value="bananas" <cfif listfind(fruit,"bananas") eq 1>checked</cfif>> bananas <input type="Checkbox" name="fruit" value="grapes" <cfif listfind(fruit,"grapes") eq 1>checked</cfif>> grapes

Steve

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 18, 2009 Sep 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>

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Sep 21, 2009 Sep 21, 2009

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 21, 2009 Sep 21, 2009
LATEST

!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources