-1

I can't quite get this to work.

My form has a number of inputs including one checkbox. In my cfquery, I just want to write some simple SQL content that tests to see if the checkbox is checked or not. But my code just ignores it completely. Here's the basics:

<cfform name="form" action="...." format="HTML">
     ....
     <cfinput type="checkbox" name="search_NR" id="search_NR" checked="no" />
    <cfinput type="submit" name="submit"  value="Search" />
</cfform>

My cfquery is quite extensive, so I'll just put the part relevant to the checkbox here:

    <cfif isDefined("form.search_NR")>
        AND (tblMain.NR = true)
    </cfif>

My thought was that the box wouldn't be defined if it wasn't checked. But whether or not I check the box on the form, the query just ignores this altogether. I just want to add tblMain.NR = TRUE to the rest of the SQL content when the box is checked.

SOS
  • 6,430
  • 2
  • 11
  • 29
stuttsdc
  • 101
  • 1
  • 4
  • 18
  • Do you have anything else that modifies the FORM scope? If you don't provide a value attribute for the checkbox, then it will send the value "on" back to the server (at least that's what it's doing with my CF8 installation). Also, are you doing anything that might cause the page to redirect, and potentially clear the FORM scope? – Dan Short Feb 03 '11 at 16:34
  • Not sure. I tried to keep this as simple as possible. I guess I didn't set a value for the checkbox but I wasn't sure what to use there, if anything. There's a lot of other stuff going on w/the form. I really just want to focus on this one box. Is it checked? If so, add this bit of code to the sql statement... – stuttsdc Feb 03 '11 at 16:38
  • 1
    Well do a super simple test to make sure it works, and then figure out what you might be doing to manipulate the form scope that's breaking it: http://pastebin.com/qZ6iP5rC – Dan Short Feb 03 '11 at 17:13
  • What is the question? – Peter Mortensen Apr 14 '18 at 08:59

4 Answers4

1
<cfif StructKeyExists(form, "search_NR")>
    AND (tblMain.NR = true)
</cfif>
Stefano D
  • 958
  • 5
  • 17
0

This will correctly display current status and allow user to change if using both <cfForm> and <cfInput type="Checkbox">:

Note: Ensure that <cfParam> tag is before other references to the form element.

<cfparam name="form.search_NR" default="off">

<cfInput name="search_NR" type="Checkbox" checked="#form.search_NR is 'on'#">Some Text Here

When the <cfform> is submitted the value of search_NR is either 'on' or 'off' and can be checked in your query. Remember to add the form. to the element name:

<cfIf form.searchNR EQ 'on'>
...
<cfElse>
...
</cfIf>
Miguel-F
  • 13,450
  • 6
  • 38
  • 63
spillbin
  • 1
  • 1
0

Try using method="POST":

<cfform name="form" action="...." format="HTML" method="post">

Otherwise your variables are submitted via the URL scope.

Sam Farmer
  • 4,108
  • 17
  • 20
  • I'm not sure what difference that makes. Right now, this page has quite a number of inputs (text boxes, listboxes, option buttons, etc) and the form calls a cfquery that looks at each of these inputs to see if they are selected or contain any info. And it all runs just fine. I'm just trying to focus on the syntax for adding a checkbox. – stuttsdc Feb 03 '11 at 17:05
  • 1
    Actually, the default for method is "post" according to the livedocs. http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=Tags_f_13.html – Dan Short Feb 03 '11 at 17:12
-2

I figured out a solution on my own...

In the form:

<input type="checkbox" name="search_NR" <cfif search_NR is "on"> checked</cfif> />

And on the processing page...

Before the cfquery:

<cfparam name="search_NR" default="off">

And in the query...

    <cfif #search_NR# EQ "on" >
            AND (tblMain.NR = true)
        </cfif>
stuttsdc
  • 101
  • 1
  • 4
  • 18
  • Did you have the value cfparam'ed before? I'm curious why you weren't getting the form field when it was checked :) – Dan Short Feb 03 '11 at 17:20
  • I don't think that was the problem... if it was, having the box unchecked would have thrown an error and I never got that. Frankly, as long as it's now working, I almost don't care. But thanks for the help. – stuttsdc Feb 03 '11 at 17:40
  • That is different than your original code which used IsDefined. The original should work 'as is', assuming method=post. That said, you should scope the variables, and get rid of the extraneous # signs. (Personally, I would explicitly set the checkbox value too.) – Leigh Feb 03 '11 at 19:12
  • is u use cfinput type="checkbox", use selected="#search_NR is 'on'#" – Henry Feb 03 '11 at 19:40