Skip to main content
Inspiring
March 9, 2009
Answered

How to preselect radio buttons

  • March 9, 2009
  • 2 replies
  • 2064 views
I use the following to dynamically generate radio buttons from my query, a list of cities.

<cfloop query="qryGetCity">
<cfoutput>
<cfinput type="radio" name="cityID" value="#qryGetCity.cityID#">#qryGetCity.city#<br>
</cfoutput>
</cfloop>

When the user logs on, I validate their information from LDAP and then they are taken to this page to select their city.

What I need to do now is when they log on, I still need to validate their info from LDAP, but also ge the city and when they are taken to this page, preselect the city for them.

How would I do this with dynamically generated radio buttons ? I think I know how to do it if hardcoded, but I dont know with dynamic output.

Thanks
    This topic has been closed for replies.
    Correct answer Newsgroup_User
    that's strange...

    to rule out any possibility of your query values messing things up, change
    iif(qryGet_Sites.site_id EQ qryGet_Sites_id.site_id
    to
    iif(qryGet_Sites.site_id EQ 1 (or any other site_id you know your query
    does return)
    and see if you still get the error.

    if you don't get the error, then the problem is with what one of your
    queries returns...
    which cf version are you on?
    which db are you using? and what is the datatype of site_id column in
    your table?
    and what does your qryGet_Sites_id return?

    Azadi Saryev
    Sabai-dee.com
    http://www.sabai-dee.com/

    try using DE('true') instead of DE('checked')?

    this is strange 'cos DE('checked') works fine in my tests...

    Azadi Saryev
    Sabai-dee.com
    http://www.sabai-dee.com/

    2 replies

    Inspiring
    March 9, 2009
    trojnfn wrote:
    >
    > How would I do this with dynamically generated radio buttons ? I think I know
    > how to do it if hardcoded, but I dont know with dynamic output.
    >


    Take what you know about hard coding it and make that dynamic. As you
    should know you put the selected='selected' parameter into the radio
    button you want the user interface to default to.

    Now you have to dynamically determine when to put that parameter on the
    radio control to which it should be attached. That sounds like a branch
    aka if statement. You can embed an entire <cfif...>...</cfif> block
    inside an <input> block, but not a <cfinput> block. You can use the
    #iif(,de(),de())# function. You can set a variable and use an
    <cfif>...</cfif> block to set the value of this variable before the
    statement. You can do some of these options with <cfscript> if (...)
    {...} code. The choice is all up to you.

    I tend to use #iif() for this type of one off branching.

    <cfinput...#iif(qry.GetCity.cityID EQ
    user.userCityId,DE("selected='selected'"),DE(""))#>
    Inspiring
    March 9, 2009
    where is the user's city information coming from? from the same ldap
    query? from somewhere else?

    wherever it is coming from, you will need to compare the city id in your
    loop to the user's city (wherever that is defined) and if matched,
    pre-select that radio button using checked="checked":

    <cfoutput query="qryGetCity">
    <cfinput type="radio" name="cityID" value="#qryGetCity.cityID#"
    #iif(qryGetCity.cityID EQ some_var_holding_current_user's_city_id,
    de('checked="checked"'), de(''))#>#qryGetCity.city#<br>
    </cfoutput>

    Azadi Saryev
    Sabai-dee.com
    http://www.sabai-dee.com/
    trojnfnAuthor
    Inspiring
    March 19, 2009
    I get the following error below regarding the # sign, my exact code is below. What am I doing wrong ?


    Invalid CFML construct found on line 492 at column 1.
    ColdFusion was looking at the following text:
    #

    The CFML compiler was processing:

    a cfinput tag beginning on line 491, column 2.
    a cfinput tag beginning on line 491, column 2.
    a cfinput tag beginning on line 491, column 2.


    The error occurred in E:\devxtroot\RecDisc\unReceivables\forms\initiate_urdn.cfm: line 492

    490 : <cfoutput query="qryGet_Sites">
    491 : <cfinput type="radio" name="site_id" value="#qryGet_Sites.site_id#"
    492 : #iif(qryGet_Sites.site_id EQ session.user_city,
    493 : DE("checked='checked'"),DE(""))#>#qryGet_Sites.site#<br>
    494 : </cfoutput>