Solved

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

Posted on 2004-08-13
4
449 Views
Last Modified: 2013-12-24
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
0
Comment
Question by:Westside2004
  • 2
4 Comments
 
LVL 6

Accepted Solution

by:
reitzen earned 175 total points
ID: 11798958
Here's the code you'll need to o show the default value when the page loads:

<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>>

That's all there is to it.

On a side note, checkboxes work the same way.

HTH
Rob
0
 
LVL 2

Expert Comment

by:russmichaels
ID: 11800283
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
0
 
LVL 1

Author Comment

by:Westside2004
ID: 11801398
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...

0
 
LVL 2

Expert Comment

by:russmichaels
ID: 11803525
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
0

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In our day to day coding, how many times have we come across a necessity to check whether a URL is a broken link or not? For those of you that answered countless and are using ColdFusion like myself, then this article is for you.  It will show yo…
Most ColdFusion developers get confused between the CFSet, Duplicate, and Structcopy methods of copying a Structure, especially which one to use when. This Article will explain the differences in the approaches with examples; therefore, after readin…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question