[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 350
  • Last Modified:

Radio Button Functionality in ColdFusion

I have a page where I call a stored procedure (SchoolOutput) in the beginning of the page.  In the stored procedure I return the value stored in a data field called schooltype.  I have a radio button group with radio buttons for each type of school.  If the datafield returns "Elementary" then I want the radio button for Elementary to be checked.  I have read the various pages that indicate to use a cfif, but I can not get that to work.  I don't know if it is a syntax error or what.  Here is what I have:

<cfoutput query="SchoolOutputOut">
  <label>
   
    <input type="radio" name="SchoolType" value="Elementary"  <cfif #schooloutputout.schooltype# eq "Elementary">checked="checked"</cfif> >
    Elementary</label>
</cfoutput>
0
mounty95
Asked:
mounty95
  • 11
  • 9
1 Solution
 
erikTsomikSystem Architect, CF programmer Commented:
what is the value of the schooltype in the database
0
 
gdemariaCommented:
How many records is schoolOutput returning?   From what you said, it should only return one record, so you don't need to loop through them.   If you are looping, then only ONE of the radio buttons will be selected because all the radio buttons use the same name.

<cfoutput>
    <input type="radio" name="SchoolType" value="Elementary"
         <cfif SchoolOutputOut.schooltype is "Elementary">checked="checked"</cfif> > Elementary
</cfoutput>

0
 
gdemariaCommented:
What you really want (I am guessing) is to loop all your different school types so you have Elementary, MIddle, High School, etc...

Then the radio button that matches your database value is selected


<cfset form.schooltype = "Elementary">  <!---- fetch current value from procedure or database ---->

getTypes is a query of all the different school types you have:  elementary, middle, high school, etc..

<cfoutput query="getTypes">
    <input type="radio" name="SchoolType" value="#getTypes.schoolType#"
         <cfif form.schooltype is getTypes.schoolType>checked="checked"</cfif> > #getTypes.schoolType#
</cfoutput>

0
Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

 
mounty95Author Commented:
I am sorry I wasn't clear.  I have radio buttons for each school type:  Elementary, Middle, High etc.

I am returning one record.  That one record has a data field which is nvarchar and will have a value of "Elementary" "Middle" or "High."

0
 
gdemariaCommented:

Yes, that is my second example... where do you get the school types from?  Are they in a database table?  If so, that is my second example.  
0
 
mounty95Author Commented:
I just tried this and this does not work:

<label>
<cfoutput query="SchoolOutputOut">
<input type="radio" name="SchoolType" value="Elementary"  
<cfif schooloutputout.schooltype is "Elementary">checked="checked"</cfif> >
Elementary</cfoutput></label>
0
 
gdemariaCommented:
Hopefully, you are not hard-coding them.   But if you are, this is an example....


<cfset form.schooltype = "Elementary">  <!---- fetch current value from procedure or database ---->

<cfloop index="aValue" list="Elementary,Middle,High">
    <input type="radio" name="SchoolType" value="#aValue#"
         <cfif form.schooltype is aValue>checked="checked"</cfif> > #aValue#
</cfloop>

         

Open in new window

0
 
mounty95Author Commented:
The school types assigned to the radio buttons are hard coded.
0
 
mounty95Author Commented:
I am not sure what the benefit one way or the other is for hard coding or not.  Can you clarify?
0
 
gdemariaCommented:
> I am not sure what the benefit one way or the other is for hard coding or not.  Can you clarify?

If you have the values in a database table, then they are stored like this...

ID   Name

1    Elementary
2    Middle School
3    High School
4    College

Then you store the ID on all the records that need to know the schoolType, for example..

Westfield School is type 2


Then you can easily search for and maintain the values.
If you change Middle School to Jr. High School, you don't have to do anything to all the other records.


This is called a foreign key relationship and is a standard way of "normalizing" a database..
0
 
mounty95Author Commented:
I actually want to get away from having a separate lookup table for the school types.  I want to hard code the possible choices so that I don't have to link between two tables.  It would be much simpler to have a group of radio buttons have them pass their values to one table that correctly identifies each record as a specific school type.
0
 
mounty95Author Commented:
<cfset form.schooltype = "Elementary">  <!---- fetch current value from procedure or database ---->
<cfloop index="aValue" list="Elementary,Middle,High">
    <input type="radio" name="SchoolType" value="#aValue#"
         <cfif form.schooltype is aValue>checked="checked"</cfif> > #aValue#
</cfloop>


So I changed the reference in line 1 to read:  <cfset form.schooltype=#schooltype#>
I am not sure how to modify lines 2-5 to have them fit my circumstances.
0
 
gdemariaCommented:
> I am not sure how to modify lines 2-5 to have them fit my circumstances.

How do they differ from your circumstances?    You should see these three radio buttons: Elementary,Middle,High... if that doesn't match then you just change the values in list="Elementary,Middle,High"


>   <cfset form.schooltype=#schooltype#>

What is the source of this variable schoolType?  You need to scope the variable and remove the #s to be proper coldfusion..


<cfset form.schooltype= getData.schooltype>
0
 
mounty95Author Commented:
<cfoutput query="SchoolOutputOut">
<cfset form.schooltype = #schooltype#>  
</cfoutput>

<cfloop index="aValue" list="Elementary,Middle,High">
    <input type="radio" name="SchoolType" value="#aValue#"
         <cfif form.schooltype is "Elementary">checked="checked"</cfif> > #aValue#
</cfloop>

This gets me three radio buttons with #aValue# as the label and none of the buttons are filled in when the returned record is "Elementary".
0
 
mounty95Author Commented:
So by adding trim to the scoping of the variable, I was able to get a button to be filled in, but now how do I get the buttons to be labeled properly for the items in the list?
0
 
gdemariaCommented:
what are the proper labels?   The code above should show..

(*)  Elementary
( )  Middle
( )  High School
0
 
gdemariaCommented:
>  This gets me three radio buttons with #aValue# as the label and none of the buttons

Whenever you see a coldfusion variable instead of it's value, it means you need to wrap it in CFOUTPUT tags


<cfoutput query="SchoolOutputOut">  <==== this format means you want to loop multiple records that are returned by schoolOutputOut.   Does that query get multiple records?   If it does, all of this is wrong.   I assume you are editing ONE school at a time.  


   <cfset form.schooltype = #schooltype#>  
</cfoutput>

<cfloop index="aValue" list="Elementary,Middle,High">
    <input type="radio" name="SchoolType" value="#aValue#"
         <cfif form.schooltype is "Elementary">checked="checked"</cfif> > #aValue#
</cfloop>


The code below should do it..

<cfoutput>  Just cfoutput, you are not looping a query here

  <cfset form.schooltype = SchoolOutputOut.schooltype>  Make sure you scope your variable!

  <cfloop index="aValue" list="Elementary,Middle,High">
      <input type="radio" name="SchoolType" value="#aValue#"
         <cfif form.schooltype is "Elementary">checked="checked"</cfif> > #aValue#
  </cfloop>

</cfoutput>

Open in new window

0
 
mounty95Author Commented:
The code returns three radio buttons all on the same line with labels of #aValue# with the last one filled in.
0
 
mounty95Author Commented:
Ok so we have three radio buttons with proper labels of Elementary, Middle, and High.

The "High" radio button is selected though and the value that should be returned is "Elementary"

Here is what I have:
<cfoutput>  

  <cfset form.schooltype = trim(#SchoolOutputOut.schooltype#)>  

  <cfloop index="aValue" list="Elementary,Middle,High">
      <input type="radio" name="SchoolType" value="#aValue#"
         <cfif form.schooltype is "Elementary">checked="checked"</cfif> > #aValue#
  </cfloop>

</cfoutput>
0
 
gdemariaCommented:
I removed the unnecessary #s from this statement...

 <cfset form.schooltype = trim(SchoolOutputOut.schooltype)>  


The CFIF should be testing the looped value  "aValue" not the hard coded "Elementary" every time

  <cfif form.schooltype is aValue>


<cfoutput>  

  <cfset form.schooltype = trim(SchoolOutputOut.schooltype)>  

  <cfloop index="aValue" list="Elementary,Middle,High">
      <input type="radio" name="SchoolType" value="#aValue#"
         <cfif form.schooltype is aValue>checked="checked"</cfif> > #aValue#
  </cfloop>

</cfoutput>

Open in new window

0
 
mounty95Author Commented:
Thanks for sticking with me!!!!  It works!!!!  Now I just need to get the value passed back into the database, but I think I can cover this.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 11
  • 9
Tackle projects and never again get stuck behind a technical roadblock.
Join Now