Solved

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

Posted on 2004-08-13
4
453 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

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

When it comes to showing a 404 error page to your visitors, you do not want that generic page to show, and you especially do not want your hosting provider’s ad error page to show either. In this article, I will show you how to enable the custom 40…
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 …
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

626 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