Solved

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

Posted on 2004-08-13
4
445 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

This is a guide to setting up a new WHM/cPanel Server to be used for web hosting accounts. It is intended for web hosting company administrators and dedicated server owners. For under $99 per month (considering normal rate of Big Data Cetnters like …
Meet the world's only “Transparent Cloud™” from Superb Internet Corporation. Now, you can experience firsthand a cloud platform that consistently outperforms Amazon Web Services (AWS), IBM’s Softlayer, and Microsoft’s Azure when it comes to CPU and …
This video discusses moving either the default database or any database to a new volume.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

771 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now