I have dynamic checkboxes that are generated as such
<cfset counter = 0>
<form name="setPermissions" class="setPermissions" action="" method="post">
<cfoutput>
<cfloop query="getUserAccess">
<input name="Meetings_#Counter#" type="checkbox" />
<cfset counter = counter + 1>
</cfloop>
</cfoutput>
</form>
The getUserAccess query has 6 rows, which means that 6 of these checkboxes will be outputted.
To read these and update the table respectively, I’m running the following query
<cfloop from="0" to="#getUserAccess.RecordCount#" index="i">
<cfquery datasource="#Request.dsn#">
UPDATE table SET
<cfif structKeyExists(FORM, 'Meetings_#i#')>
Meetings = <cfif FORM['Meetings_#i#'] EQ "on">1,<cfelse>0,</cfif>
</cfif>
WHERE ID = '#an_ID_that_is_specified#'
</cfquery>
</cfloop>
Apologies if this code isn’t correct, it works when I’m running it on my page, but there are about 20 of these checkboxes being generated, so I’ve snipped my code so you can see only one.
Now, this code works great, when I check a box and submit the form, this code will update the database to set the value to 1.
The only problem is, when I uncheck the box, it won’t change the value to a 0. This is where I am stuck. Why is it not changing the value to 0 when there is no on specified as a form value?
I’ve done a <cfdump> to see if anything was still being passed, but there isn’t.
With all of the boxes ticked I get this dump
MEETINGS_0 on
MEETINGS_1 on
MEETINGS_2 on
MEETINGS_3 on
MEETINGS_4 on
MEETINGS_5 on
With some ticked, the ones which I haven’t ticked are not showing up.
However the value is still not updating from a 1 to a 0.
Checkbox simply wont submit any value when unchecked (that’s how it works). So your query should look closer to this: