Skip to main content
Participant
January 15, 2007
Question

How to check if a specific radio button value is selected

  • January 15, 2007
  • 6 replies
  • 5642 views
Hi,

I am a newbie and need some help with validating radio button values. Basically, I need to set parameter values based on which radio button is selected. Below is my code but it doesn't work - I think "<cfif #price# is "l_350">" is wrong.
Please help!!!!

Thanks a lot in advance!

HTML:
<radio name="price" value="l_350"> $350
<radio name="price" value="m_300"> $300
<radio name="price" value="s_250"> $250

CF:
<cfif #price# is "l_350">
<cfparam name="t1" default="350">
<cfelse>
<cfparam name="t1" default="0">
</cfif>
<cfif #price# is "m_300">
<cfparam name="t1" default="300">
<cfelse>
<cfparam name="t1" default="0">
</cfif>... so on.
This topic has been closed for replies.

6 replies

Inspiring
January 16, 2007
Oooo MaryJo, your code just makes me tingle all over. :^)
Inspiring
January 16, 2007
quote:

Originally posted by: Dinghus
Oooo MaryJo, your code just makes me tingle all over. :^)

LOL, glad you enjoyed it. ;-) I've been using iif() a lot recently so had it on the brain, and that issue when doing an isDefined() test drove me nuts in the past. Of course, the trick is knowing when best to use this. It's going to run a little slower than a simple cfif-else statement, so generally only use on pages that will not get run a lot in production. For my software, that generally means I use it mostly on the admin back-end functions.

BKBK
Community Expert
Community Expert
January 15, 2007
> That will crash is form.price is not defined.

I don't think so. If form.price is not defined, Coldfusion will skip the part 'and form.price is "l_350"' and will just 'do that'.



Inspiring
January 16, 2007
quote:

Originally posted by: BKBK
I don't think so. If form.price is not defined, Coldfusion will skip the part 'and form.price is "l_350"' and will just 'do that'.


Correct. This is known as short-circuit evaluation and has been available in ColdFusion since version 4.01 I believe. It was quite a pain to code around before then. ;-)

Inspiring
January 16, 2007
I thought I'd add that the iif() doesn't quite follow the same rule, due to the way it evaluates the strings. So for instance this will throw an error if form.price is not defined:

<cfset Price = iif(isDefined("form.price"), form.price, 0)>

So you have to throw an Evaluate() and DE() around the string to get it to work:

<cfset Price = iif(isDefined("form.price"), Evaluate(DE(form.price)), 0)>

This code is a short-cut way to set a default value for a form variable if it does not get passed.
BKBK
Community Expert
Community Expert
January 15, 2007
<radio name="price" value="l_350"> $350
<radio name="price" value="m_300"> $300
<radio name="price" value="s_250"> $250


I would use instead

<INPUT type="radio" name="price" value="l_350"> $350<br>
<INPUT type="radio" name="price" value="m_300"> $300<br>
<INPUT type="radio" name="price" value="s_250"> $250<br>

Can I do like this?
<cfif #isdefined("price")# is "l_350">
do this
<cfelse>
do that
</cfif>


Do instead

<cfif isdefined("form.price") and form.price is "l_350">
do this
<cfelse>
do that
</cfif>


Inspiring
January 15, 2007
quote:

Originally posted by: BKBK
Can I do like this?
<cfif #isdefined("price")# is "l_350">
do this
<cfelse>
do that
</cfif>


Do instead
<cfif isdefined("form.price") and form.price is "l_350">
do this
<cfelse>
do that
</cfif>


That will crash is form.price is not defined.
Inspiring
January 15, 2007
no. is defined returns true or false so it will never be l_350.
Inspiring
January 15, 2007
quote:

Originally posted by: Janggu2
Hi,
I am a newbie and need some help with validating radio button values. Basically, I need to set parameter values based on which radio button is selected. Below is my code but it doesn't work - I think "<cfif #price# is "l_350">" is wrong.
Please help!!!!
Thanks a lot in advance!
HTML:
<radio name="price" value="l_350"> $350
<radio name="price" value="m_300"> $300
<radio name="price" value="s_250"> $250

CF:
<cfif #price# is "l_350">
<cfparam name="t1" default="350">
<cfelse>
<cfparam name="t1" default="0">
</cfif>
<cfif #price# is "m_300">
<cfparam name="t1" default="300">
<cfelse>
<cfparam name="t1" default="0">
</cfif>... so on.

Is the cf code you have on the form page or on the page to which the page submits? Assuming it's on the page to which the form submits, my comments are:

-switch/case is more appropriate than if/else
-you don't need octothorps inside a cfif tag.
-you should qualify your variables with the form scope.
-cfset is a more appropriate tag than cfparam to for variable t1.

Inspiring
January 15, 2007
A radio button does not really exist until it is selected. You can use

<cfif #isdefined("price")#>
Janggu2Author
Participant
January 15, 2007
Can I do like this?

<cfif #isdefined("price")# is "l_350">
do this
<cfelse>
do that
</cfif>