Retrieve radio buttons value from a database and display them on the screen.

This tutorial explains how to save a value from several radio buttons to a database and how to retrieve the value and set the radio button choice so their choice is selected when return to the page.

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 the checked radio button will be saved.

Field Name Data Type
Table_ID AutoNumber
Pet_Name Text

Display the Radio Buttons dialog page. (enter.cfm)

<!--- Set all radio buttons 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>

<cfswitch expression = "#read_db.Pet_Name#">

<!--- Case db value is Pig, set Pig radio box checked. --->

<cfcase value="Pig">

<cfset Pig_Checked = "checked">

</cfcase>

<!--- Case db value is Bird, set Bird radio box checked. --->

<cfcase value="Bird">

<cfset Bird_Checked = "checked">

</cfcase>

<!--- Case db value is Fish, set Fish radio box checked. --->

<cfcase value="Fish">

<cfset Fish_Checked = "checked">

</cfcase>

<!--- Case db value is Cat, set Cat radio box checked. --->

<cfcase value="Cat">

<cfset Cat_Checked = "checked">

</cfcase>

<!--- Case db value is Dog, set Dog radio box checked. --->

<cfcase value="Dog">

<cfset Dog_Checked = "checked">

</cfcase>

<!--- Case db value is Rabbit, set Rabbit radio box checked. --->

<cfcase value="Rabbit">

<cfset Rabbit_Checked = "checked">

</cfcase>

<!--- Set Pig default checked. --->

<cfdefaultcase>

<cfset Pig_Checked = "checked">

</cfdefaultcase>

</cfswitch>

What is your favorite pet?<br>

<form action="save.cfm" method="post" name="pet">

Pig:

<input name="fovorite_pet" type="radio" value="Pig" <cfoutput>#Pig_Checked#</cfoutput>>

<br>

Bird:

<input name="fovorite_pet" type="radio" value="Bird" <cfoutput>#Bird_Checked#</cfoutput>>

<br>

Fish:

<input name="fovorite_pet" type="radio" value="Fish" <cfoutput>#Fish_Checked#</cfoutput>>

<br>

Cat:

<input name="fovorite_pet" type="radio" value="Cat" <cfoutput>#Cat_Checked#</cfoutput>>

<br>

Dog:

<input name="fovorite_pet" type="radio" value="Dog" <cfoutput>#Dog_Checked#</cfoutput>>

<br>

Rabbit:

<input name="fovorite_pet" type="radio" value="Rabbit" <cfoutput>#Rabbit_Checked#</cfoutput>>

<br>

<input name="submit_button" type="submit">

</form>

Save the value of the checked radio button to the database. (save.cfm)

<!--- Output your favorite pet. --->

Your favorite pet is <cfoutput>#Form.fovorite_pet#</cfoutput><br>

<!--- Open the database and save the value. --->

<cfquery name="update_db" datasource="pet">

UPDATE pet SET Pet_Name = '#Form.fovorite_pet#'

WHERE Table_ID = 1

</cfquery>

<!--- Display the button to go back to Radio page. --->

<form method="post" action="enter.cfm">

<input name="submit_button" type="submit" value="Read from db">

</form>

Advertisement

Atom Entertainment (formerly AtomShockwave)

No User Comments.

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

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