Skip to main content
Participating Frequently
September 10, 2010
Question

Dynamic Radio Button Group

  • September 10, 2010
  • 3 replies
  • 2260 views
I need to get the value of the radio button, if the user selected Y or 
N. The Field names are going to be dynamically generated from id's in
the database to form Radio Groups. The code below will not work, but you can
see what I trying to achieve, any ideas?


<cfif
isDefined('FORM.bnt2')>
   
<cfloop index="i" list="#form.fieldnames#">
       
<cfquery  name="accept" datasource="#request.dsn#">
            UPDATE opt SET
            f14 = '#evaluate(i)#'
            WHERE f4 = '#FORM.id#'
       
</cfquery>
   
</cfloop>
</cfif>                                                             

<cfform  name="frm2" id="form2" method="post" action="">
   
<cfinput type="radio" name="#f5#" value="y">
   
<cfinput type="radio" name="#f5#" value="n">
   
<cfinput type="hidden" name="id" value="#f4#" />
   
<cfinput name="bnt2" type="image" class="btn" src="images/accept.gif" value="Submit"/>
    This topic has been closed for replies.

    3 replies

    Participating Frequently
    September 11, 2010

    Thank you all for your help, here was my final solution.

    <cfif isDefined("FORM.bnt2")>
    <cfloop index="id" list="#form.listofids#" delimiters=",">
    <cfquery  name="accept" datasource="#request.dsn#">
    UPDATE opt SET
    F14 = '#Evaluate("form.select_#id#")#'
    WHERE f5 = '#id#'
    </cfquery>
    </cfloop>
    </cfif>

    <cfform  name="frm2" id="form2" method="post" action="">
    <cfoutput query="accountCheck" >
    <cfinput type="radio" name="select_#f5#" value="y" required="yes" validateat="onsubmit" message="A value of Yes or No is required.">
    <cfinput type="radio" name="select_#f5#" value="n" required="yes" validateat="onsubmit" message="A value of Yes or No is required.">
    <cfinput type="hidden" name="listofids" value="#ValueList(accountCheck.f5)#" >
    </cfoutput>
    <cfinput name="bnt2" type="image" class="btn" src="images/accept.gif" value="x"  style="float:right;" />
    </cfform>

    Inspiring
    September 10, 2010

    Couple of things.

    First, array notation runs faster than evaluate:  the syntax is form["static part" & variable part]

    Second, look at your data before trying to use it in a query.

    Third, according to your code, the database is being updated.  However, it's being updated for every single form field.

    Fourth, on your form, life is a lot simpler if you give every radio button the same name, but different values.

    Participating Frequently
    September 10, 2010

    Hi Dan,

    That is my problem I need to update only for the radio buttons and don't know how. However, I cannot give the radio buttons a static name

    that would make this task a sinch if I could. The problem is I could have 1 radio group retuned or 10 radio groups returned,

    in the case if the user has 10 accounts I need some way to return 10 different radio group names to make the Y N options

    mutually exclusive.

    Inspiring
    September 10, 2010

    Regarding:  However, I cannot give the radio buttons a static name

    Why not?

    Inspiring
    September 10, 2010

    The code below will not work

    Can you elaborate on "will not work"? If you are receiving an error, can you post the full error message?

    Participating Frequently
    September 10, 2010

    Thank you for your response, here is a little background on what I'm trying to achive.

    The user will input their Id number then the accounts tied to this Id  number will be presented(radio buttons) . They could have 1 or even 10  accounts returned(radio buttons). I need to have them update their account to a Y, N or if empty do not enter a value. The  account Id is what makes up the radio groups. Now I need to know how to  loop through these radio buttons to see what they selected.

    So when I say will not work, I mean I cannot get the value of the radio buttons to update the db.

    Inspiring
    September 10, 2010

    >>

    What is an example of the names #f5# and how does it relate to the "id" value #f4#?

    In terms of basic form mechanics, nothing will be passed if neither button is selected. It does not look like you are forcing Y or N to be checked by default. So what do you want to happen if the user does not check either button?

    -Leigh