The best way to work with CFINPUT type=”checkbox” with MySQL

checkbox-1Since I began using CFML I’ve struggled with this problem every time I want to add a checkbox to a form that updates a database field.

The problem is that if the checkbox isn’t checked then no evidence of its existence is passed to the form’s action page. This causes a problem when updating a database field because you can’t rely on the FORM structure to provide a value of “no” or “off” that can be updated in your db field.
You therefore need to use something like this before your <cfupdate..>, <cfinsert…> or <cfquery…> tags.:

 <cfparam name="FORM.mycheckbox" default="off">

Similarly when populating a form containing a checkbox you need to use a plain <input type=”checkbox”> HTML tag instead of the CFINPUT version. This then allows you to insert <cfif> tag like so…

<input type="checkbox" name="myCheckbox" 
	<cfif #dbQuery.myCheckbox# eq 'on'> 

The db field must then be either a string-based data type or an enum type with ‘on’ and ‘off’ as its possible values.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>