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>
mounty95Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Servers

From novice to tech pro — start learning today.