Copy link to clipboard
Copied
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!
Database table: fruitChoice
userID | apple | orange | banana | grape |
---|---|---|---|---|
x147d | 1 | 0 | 1 | 1 |
a065b | 0 | 1 | 1 | 1 |
t992w | 1 | 0 | 1 | 0 |
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
...Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
Database table: fruitChoice
userID | apple | orange | banana | grape |
---|---|---|---|---|
x147d | 1 | 0 | 1 | 1 |
a065b | 0 | 1 | 1 | 1 |
t992w | 1 | 0 | 1 | 0 |
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>
Copy link to clipboard
Copied
Thank you so much for taking your time. It worked perfectly!
Copy link to clipboard
Copied
!