Cfinput checkbox checked

12.4k views Asked by At

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.

4

There are 4 answers

2
Sam Farmer On

Try using method="POST":

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

Otherwise your variables are submitted via the URL scope.

4
stuttsdc On

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>
0
Stefano D On
<cfif StructKeyExists(form, "search_NR")>
    AND (tblMain.NR = true)
</cfif>
0
spillbin On

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>