Link to home
Start Free TrialLog in
Avatar of Westside2004
Westside2004Flag for United States of America

asked on

Preselected/PreFilled radio button based on data stored in our db

Hi,

I have a query that retrieves user information from our db.

This information or data is displayed on an edit page.  Basically we query our db to get the users information that they wish to edit.  We pre-fill the form fields accordingly.

I am able to prefill text boxes and drop downs fine, but how can I pre-select a radio button based on the query?

So if there are two radio buttons, typeA and typeB, and user chose typeB initially.  Then the user wants to edit their record, so they click the edit link, the page is displayed with all the users info pre-filled.  How can I prefill the radio button?

currently my radio button looks like:

<input name="locationType" type="radio" value="TypeB">

For text boxes I do a <cfoutput>#FieldName#</cfoutput> in the value attribute of the input tag, but in the case of the radio button, I have already used the value attribute if that makes any sense..

Help highly appreciated as always

-West
ASKER CERTIFIED SOLUTION
Avatar of reitzen
reitzen
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of russmichaels
russmichaels

I like to use the IIF function, you will find that this will not get screwed up by html editors or page cleanup utils. Sometimes putting a cftag inside an HTML tag can cause problems. I also personally find it more readble.

The IIF function works thus

IIF(condition, true, false)

condition = the condition you want to test
true = what to display/do is condition is true
false = what to display/do if condition is false.

So where you want SELECTED to appear in the tag, you put this.

#IIF(columnNameis 'value', DE('selected'), DE(''))#

Replacing ColumnName with your database column, and value with the value you are checking it is equal to.

E.G.

<input name="locationType" type="radio" value="TypeB" #IIF(LocationType is 'typeA', DE('selected'), DE(''))#>

The DE() function means Delayed evaluation, you can thus use it to display text without trying to ecvaluate a variable. If you do not use DE() then whatever value you have for the true/false coldfusion will try to evaluate.

So where the value of locationType = typeA, the text SELECTED is inserted into the tag, otherwise nothing is inserted.

Regards

Russ Michaels
Avatar of Westside2004

ASKER

Hi,

Thanks for the replies..

I tried your code:

<input type="radio" name="locationType" value="typeA"<cfif myQuery.locationType EQ "typeA"> selected</cfif>>
<br />
<input type="radio" name="locationType" value="typeB"<cfif myQuery.locationType EQ "typeB"> selected</cfif>>

It did not work, but I got it to work my changing the "selected" to "checked"

I am using radio buttons as you know so "selected" did not seem to work. I changed it to "checked" and it worked..

Also, thanks for the solution Russ, I did not try it, because it seems like a bit more code.. I have heard mixed things about using IIF, but since the other way worked, figured I would go with that..

Thanks however and if I run into problems.. will try your solution...

Regards,

-West...

Non programmers usually prefer to use the tags and shy away from CFSCRIPT or complex looking functions, so that's fine. It doesn't really make much difference in this case.
not sure where you get the idea it's more mode though.
If you talking about how many key strokes it takes to type the code

The CFIF way is 52 key strokes
the IIF way is 49 keystrokes

so it's actually less.

Anyway, the comments you will have heard about IIF is that it is slower than CFIF. Which technically can be true. But in order for this to be noticed or make a difference you would need to have hundreds of IIF statements on the same page to see a few milliseconds difference between using IIF and CFIF. A millisecond is 1000th of a second, so as you see it really makes no difference.
Usually the people who make this comment and say do not use it, do not even optimise their own code and are just trying to be clever, or they are obsessive about saving every possible MS of processing time.
So don't it too seriously when people tell you about not using certain tags or funcitons. Try it for yourself and see how it affects your particular application.

Russ