Checkbox in a ColdFusion form

跟風遠走 提交于 2019-12-02 05:15:52

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>

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>

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">

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>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!