ColdFusion Tutorial

Retrieve check boxes values from a database and display them on the screen.

This tutorial explains how to save several check boxes to a database and how to retrieve the value of each check box from the database and display them on the screen with checked or unchecked.

First we create the database for this example (Table = pet).

(Remember to set the Field Name: Table_ID as Primary Key)

In this Access database, the value of Check Box will be stored as 0 or 1. Where 0 is False - not checked and 1 is True - checked.

Field Name Data Type
Table_ID AutoNumber
Pig Yes/No
Bird Yes/No
Fish Yes/No
Cat Yes/No
Dog Yes/No
Rabbit Yes/No

Display the Check Box dialog page. (enter.cfm)

<!--- Set all Check Boxes unchecked. --->
<cfset Pig_Checked ="">
<cfset Bird_Checked ="">
<cfset Fish_Checked ="">
<cfset Cat_Checked ="">
<cfset Dog_Checked ="">
<cfset Rabbit_Checked ="">
<!--- Open the database and read the value of each check box. --->
<!--- (PS: We use Table_ID = 1, so we always read/update the same table.) --->

<cfquery name="read_db" datasource="pet">
SELECT * FROM pet WHERE Table_ID = 1
</cfquery>
<cfoutput query="read_db">
<!--- If Check Box Pig is True, set this Check Box checked. --->
<cfif #Pig#>
<cfset Pig_Checked ="checked">
</cfif>
<!--- If Check Box Bird is True, set this Check Box checked. --->
<cfif #Bird#>
<cfset Bird_Checked ="checked">
</cfif>
<!--- If Check Box Fish is True, set this Check Box checked. --->
<cfif #Fish#>
<cfset Fish_Checked ="checked">
</cfif>
<!--- If Check Box Cat is True, set this Check Box checked. --->
<cfif #Cat#>
<cfset Cat_Checked ="checked">
</cfif>
<!--- If Check Box Dog is True, set this Check Box checked. --->
<cfif #Dog#>
<cfset Dog_Checked ="checked">
</cfif>
<!--- If Check Box Rabbit is True, set this Check Box checked. --->
<cfif #Rabbit#>
<cfset Rabbit_Checked ="checked">
</cfif>
</cfoutput>
<!--- Display the form on the screen. --->
<form method="post" action="save.cfm">
<input name="fovorite_pet" type="checkbox" value="pig" <cfoutput>#Pig_Checked#</cfoutput>>
Pig<br>
<input name="fovorite_pet" type="checkbox" value="bird" <cfoutput>#Bird_Checked#</cfoutput>>
Bird<br>
<input name="fovorite_pet" type="checkbox" value="fish" <cfoutput>#Fish_Checked#</cfoutput>>
Fish<br>
<input name="fovorite_pet" type="checkbox" value="cat" <cfoutput>#Cat_Checked#</cfoutput>>
Cat<br>
<input name="fovorite_pet" type="checkbox" value="dog" <cfoutput>#Dog_Checked#</cfoutput>>
Dog <br>
<input name="fovorite_pet" type="checkbox" value="rabbit" <cfoutput>#Rabbit_Checked#</cfoutput>>
Rabbit <br>
<input name="submit_button" type="submit" value="Save to db">
</form>

Save the value of each Check Box to the database. (save.cfm)

<!--- Set every Check Box False (no value or unchecked). --->
<cfset Bolean_Pig = False>
<cfset Bolean_Bird = False>
<cfset Bolean_Fish = False>
<cfset Bolean_Cat = False>
<cfset Bolean_Dog = False>
<cfset Bolean_Rabbit = False>
<cfif NOT isDefined("Form.fovorite_pet")>
<cfelse>
<!--- If Pig is Checked, set this value like true. --->
<cfif listContains("#Form.fovorite_pet#","pig")>
<cfset Bolean_Pig = True>
</cfif>
<!--- If Bird is Checked, set this value like true. --->
<cfif listContains("#Form.fovorite_pet#","bird")>
<cfset Bolean_Bird = True>
</cfif>
<!--- If Fish is Checked, set this value like true. --->
<cfif listContains("#Form.fovorite_pet#","fish")>
<cfset Bolean_Fish = True>
</cfif>
<!--- If Cat is Checked, set this value like true. --->
<cfif listContains("#Form.fovorite_pet#","cat")>
<cfset Bolean_Cat = True>
</cfif>
<!--- If Dog is Checked, set this value like true. --->
<cfif listContains("#Form.fovorite_pet#","dog")>
<cfset Bolean_Dog = True>
</cfif>
<!--- If Rabbit is Checked, set this value like true. --->
<cfif listContains("#Form.fovorite_pet#","rabbit")>
<cfset Bolean_Rabbit = True>
</cfif>
</cfif>
<!--- Open the database and save the values. --->
<cfquery name="update_db" datasource="pet">
UPDATE pet SET Pig = #Bolean_Pig#,
Bird = #Bolean_Bird#,
Fish = #Bolean_Fish#,
Cat = #Bolean_Cat#,
Dog = #Bolean_Dog#,
Rabbit = #Bolean_Rabbit#
WHERE Table_ID = 1
</cfquery>
<!--- Display the button to go back to Check Box page. --->
<form method="post" action="enter.cfm">
<input name="submit_button" type="submit" value="Read from db">
</form>
 

ColdFusion Tutorial test

Retrieve check boxes values from a database and display them on the screen.

No test is available or implemented for this ColdFusion Tutorial.

No User Comments.

No User Comments, be the first one to write your comments?

Add your comments

Your Name*:
Your Email:
Site URL:
Site Name:
Comemt Title*:
Your Comment*:
Key Phrase*:
wS0
Your IP Address: 38.103.63.62
 

ScandicWeb is own by Scandic Systems LTD [UK] Company No. 5984000. All other trademarks and copyrights are the property of their respective holders.

[Home [Site Map [Privacy Statement [Site Feedback] [About ScandicWeb]


[ColdFusion [Flash [Search Engine Optimization [Graphics [Cascading Style Sheets (CSS) [JavaScript (JS) [Software]

Partner sites: ScandicSoft


Server time: 19. November 2008 Wednesday