Solved

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

Posted on 2004-08-13
4
448 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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

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 …
One of the typical problems I have experienced is when you have to move a web server from one hosting site to another. You normally prepare all on the new host, transfer the site, change DNS and cross your fingers hoping all will be ok on new server…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

777 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