Skip to main content
Inspiring
July 28, 2006
Answered

Assign Session Variables to Radio Buttons

Sorry if this may have been partially answered in another post, but I can seem to find the answer.

I have a form that contains radio buttons, along with other form fields. If the user hits the submit button, it takes them to a processing page which has alot of validation checks, spits an error message out to the user, and displays a 'go back' button to return to the previous page for corrections.

All of my text input boxes have session variables assigned to the 'value' field and they all work great. After the user hits the go back button, the data is re-populated back in the fields. However, I cannot figure out how to assign a session variable to my radio buttons. Here is my code:

<cfinput required="yes" message="You must Indicate Y or N for timeframe - work during C-Check" type="radio" value="YES" name="ccheck">YES
<cfinput required="yes" message="You must Indicate Y or N for timeframe - work during C-Check" type="radio" value="NO" name="ccheck">NO

You can see that unlike text boxes, the value field is already populated, so I am confused as to where to add the session variable. Here is what my text input field looks like:

<cfinput type="text" name="ccheck_total" size="8" validate="integer" message="The Total Aircraft field can only contain numbers." value="#session.ccheck_total#">

I'm fairly sure I will need an <cfif> statement on my processing page, but am confused how to code it. Please assist.
    This topic is closed to new replies. Start a new post to keep the conversation going.
    Kabuto2Correct answer
    Participating Frequently
    July 28, 2006
    In HTML you just add CHECKED to the <INPUT> line. I would think off hand cfinput is similar. You could then cfif CHECKED based on the session value (YES or NO)

    eg. <input type=radio name="ccheck" value="YES" <CFIF session.variable EQ 'YES'>checked</CFIF>....

    EDIT: Err what he said. :)

    7 replies

    LumpiaAuthor
    Inspiring
    July 31, 2006
    Thanks everyone for your replies. They all helped very much. I would like to say that regarding nesting <cfif> statements... they cannot be nested within a <cfinput> tag. You can nest a <cfif> statement within a normal <input> tag, which happened to solve my problem. Again, thanks everyone for their comments/suggestions as they all helped.
    Translate
    Kabuto2Correct answer
    Participating Frequently
    July 28, 2006
    In HTML you just add CHECKED to the <INPUT> line. I would think off hand cfinput is similar. You could then cfif CHECKED based on the session value (YES or NO)

    eg. <input type=radio name="ccheck" value="YES" <CFIF session.variable EQ 'YES'>checked</CFIF>....

    EDIT: Err what he said. :)
    Translate
    Inspiring
    July 28, 2006
    Just as a side note, the correct syntax for checked radio button is

    checked="checked"

    checked on its own will work but it is not XHTML compliant!
    Translate
    Inspiring
    July 28, 2006
    First, I would set a variable incase your session is not defined somewhere above the radiobutton:
    <CFIF NOT Isdefined("Session.ccheck")>
    <CFSEt ccheck = "">
    <CFELSE>
    <CFSET ccheck = session.ccheck>
    </CFIF>

    Then in your radio buttons:
    <cfinput required="yes" message="You must Indicate Y or N for timeframe - work during C-Check" type="radio" value="YES" name="ccheck"<CFIF ccheck IS "Yes"> Checked</CFIF>>Yes
    <cfinput required="yes" message="You must Indicate Y or N for timeframe - work during C-Check" type="radio" value="NO" name="ccheck""<CFIF ccheck IS "No"> Checked</CFIF>>NO
    Translate
    LumpiaAuthor
    Inspiring
    July 28, 2006
    For whatever reason, I cannot get this to work. I have tried numerous variations of this code and no luck. Any other ideas?
    Translate
    Inspiring
    July 28, 2006
    Output session.ccheck to what the value is. Maby that's not being set right or you have to adjust your CF settings.

    Translate