user747291
user747291

Reputation: 821

Checkbox in a ColdFusion form

My code is below. I need to have both of the checkboxes checked by default when the page loads. This displays a result of the query. Now when one of the checkboxes is unchecked the form needs to be submitted and different query results need to be displayed. The checkboxes are always being checked even when I uncheck one. Can someone please guide me here?

<form action="abc.cfm?show=yes" method="post" name="myform">
    <table align="center">
    <tr>
        <td>
            <input type="checkbox" checked="checked" name="chkbox" id="chkbox1"> <strong> Agreement Only</strong> 
            &nbsp;&nbsp;<input type="hidden" name="chk" id="chk1">
            <input type="checkbox" checked="checked" name="chkbox" id="chkbox2"> <strong>Active Employees</strong> 
            &nbsp;&nbsp;<input type="hidden" name="chk" id="chk2">
        </td>
        <td>
            <input type="Submit" name="submitnow" value="View now">
        </td>
    </table>
</form>

<cfif isdefined("form.chk1")>
    query 1
<cfelseif isdefined("form.chk2")>
    query 2
</cfif>

Upvotes: 1

Views: 8373

Answers (3)

steve
steve

Reputation: 1490

There are multiple ways you could accomplish what your trying to do. I wasn't sure what the purpose of your hidden fields were, so I modified a few things to try to make life a little easier.

Some people may suggest structkeyexists, but I didnt want to introduce a new command that you may not be familiar with.

<cfparam name="form.chkbox" default="">

<form action="abc.cfm?show=yes" method="post" name="myform">
  <table align="center">
    <tr>
      <td><input type="checkbox" <cfif form.chkbox eq "" or listfind(form.chkbox, 1)>checked="checked"</cfif> name="chkbox" id="chkbox1" value="1">
        <strong> Agreement                        Only</strong> &nbsp;&nbsp;
          <input type="checkbox" <cfif form.chkbox eq "" or listfind(form.chkbox, 2)>checked="checked"</cfif> name="chkbox" id="chkbox1" value="2">
        <strong>Active                  Employees</strong> &nbsp;&nbsp;
</td>
      <td><input type="Submit" name="submitnow" value="View now"></td>
  </table>
</form>
<cfif listfind(form.chkbox, 1) and listfind(form.chkbox,2)>
  query 1
  query 2
</cfif>

Upvotes: 1

Joe C
Joe C

Reputation: 3546

The problem is that the inputs (chk1 and chk2) DO exist in the form scope when the form is submitted. Their values, however, are empty.

To demonstrate this, dump out the form scope before your isdefined checks.

<cfdump var="#form#" label="form scope">

form scope dump

You need to be checking for the value or length attributes of the inputs.

<cfif len(form.chk1)>
    query 1
</cfif>

BUT!

This doesn't actually seem to be what you want to do. chk1 and chk2 are your TEXT inputs, not your CHECKBOX inputs.

If you want to be acting based on the checkboxes, you need to be checking the value of the checkbox inputs - which brings us to another issue: you didn't set a value attribute for the checkboxes.

<input type="checkbox" value="1"...>

Now, you need to param the values of these checkboxes to ensure that fields exist at all

<cfparam name="form.chkbox1" value="0">

And then check the value

<cfif form.chkbox1 EQ 1>
    query 1
</cfif>

Upvotes: 0

Matt Busche
Matt Busche

Reputation: 14333

you've named the checkboxes the same thing and are always checking them, so why would they not be checked?

You need to name them uniquely and check if the key exists in the form once the page has been submitted. Or display the box as checked when the form has not been submitted

The form has not been submitted - NOT structKeyExists(form,'fieldnames')

The form has been submitted and chkbox1 was selected - structKeyExists(form,'chkbox1')

 <td>
   <input type="checkbox"<cfif NOT structKeyExists(form,'fieldnames') OR structKeyExists(form,'chkbox1')> checked="checked"</cfif> name="chkbox1" id="chkbox1"> <strong> Agreement                        Only</strong> 
    &nbsp;&nbsp;<input type="hidden" name="chk" id="chk1">
     <input type="checkbox"<cfif NOT structKeyExists(form,'fieldnames') OR structKeyExists(form,'chkbox2')> checked="checked"</cfif> name="chkbox2" id="chkbox2"> <strong>Active                  Employees</strong> 
   &nbsp;&nbsp;<input type="hidden" name="chk" id="chk2">
  </td>

Upvotes: 6

Related Questions