Ruchi Matharu
Ruchi Matharu

Reputation: 51

how to insert single checkbox value in coldfusion code

I have a single checkbox, I want that when I check the checkbox it should insert 1 else 0 in the database. How can I do that? This was earlier a radio button field which is getting converted to check box so already entry in the database is working good, I am posting my database code as well.

    <tr>
            <td class="leftFormLabelCell extrasmalltextbold" style="border-
             left:1px solid ##9c9c9c;" width="15%">
             #mocTrans.Translate("Required template Action Item?")#
            </td>

            <td>
               <input type="checkbox" name="reqtempactionitem" value="0">
            </td>
    </tr>
    Databse code: 
    <cfif StructKeyExists(URL, "reqtempactionitem") and 
     IsBoolean(URL.reqtempactionitem)>
    , #reqtempactionitem#
    <cfelse>
    , 0
    </cfif>

Upvotes: 0

Views: 328

Answers (1)

Tim Jasko
Tim Jasko

Reputation: 1542

The way html checkboxes work is that if checked, the browser will submit [checkboxname]=[value] to the webserver. If the box is not checked, the browser does not submit anything at all to the server.

So the easiest solution uses cfparam, which will give the submitted checkbox a default value.

Thus, in your html, you should have:

<input type="checkbox" name="reqtempactionitem" value="1">

(As has been noted in comments, your value was 0 and should be 1.)

Then, in the database code:

<cfparam name="reqtempactionitem" default="0">
...
dbfield = <cfqueryparam cfsqltype="cf_sql_bit" value="#reqtempactionitem#">

Note the use of cfqueryparam, which is strongly recommended in all queries for both performance and security reasons.

Upvotes: 2

Related Questions