Search code examples
formsgetcoldfusionradio-button

How to get the value of radio button in ColdFusion?


I can't find a simple tutorial to help me do this task. Basically I need to assign the value of radio button and pass it to the next page through session. This is my code for the radio button input.

<input type="radio" name="statReqYN" id="statReqYN-0" value="Yes" checked="checked"> Yes
<input type="radio" name="statReqYN" id="statReqYN-1" value="No"> No

After that, I will set the value of button:

<cfif isdefined("form.newProdYN") and form.newProdYN is "No"> 
<cfset form.newProdNY = "No">
</cfif>

Lastly, I will pass it to the next page of the same session through the submit button:

<cfif not arrayLen(errors)>
<cfset session.checkout.input = {
newProdNY=form.newProdNY}>
<cflocation url="formcomplete.cfm" addToken="false">
</cfif>

But when I try to get the value with #session.checkout.input.newProdYN# in html, the result is undefined. Can anyone help me solve this problem?


Solution

  • Your question isn't very clear as there are variables that are not being shown in your code. Generally, with radio and checkbox fields, your receiving form should have a default value set. I do this by doing something along the lines of:

    <cfparam name="FORM.statReqYN" default="no">

    This way you can always use the variable. So in your case, I would have this as the whole template:

    <cfparam name="form.statReqYN" default="No">
    <cfparam name="form.newProdYN" default="Yes">
    
    <form action="" method="post">
        <input type="radio" name="statReqYN" id="statReqYN-0" value="Yes" checked="checked"> Yes
        <input type="radio" name="statReqYN" id="statReqYN-1" value="No"> No
    
        <button type="submit" name="newProdYN" value="Yes">Submit</button>
    
    </form>
    
    
    <cfif form.newProdYN IS 'Yes'>
        <cfset session.checkout.input.newProduNY = form.newProdYN >
        <cfset session.checkout.input.statReqYN = form.statReqYN >
        <cflocation url="formcomplete.cfm" addToken="false">
    </cfif>
    

    I hope this makes a bit more sense?