如何在Coldfusion代码中插入单个复选框值 [英] how to insert single checkbox value in coldfusion code

查看:69
本文介绍了如何在Coldfusion代码中插入单个复选框值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个复选框,我希望当我选中该复选框时,
应该在数据库中插入1否则为0。我怎样才能做到这一点?
之前是一个单选按钮字段,该字段已转换为复选框,因此数据库中已经输入的
正常工作,我也正在发布数据库
代码。

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>


推荐答案

html复选框的工作方式是:如果选中,浏览器将向网络服务器提交 [复选框名称] = [值] 。如果未选中该框,则浏览器根本不会向服务器提交任何内容。

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.

因此最简单的解决方案使用 cfparam ,这将为提交的复选框提供默认值。

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

因此,在您的html中,您应该具有:

Thus, in your html, you should have:

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

(如注释中所述,您的值为0,应为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#">

请注意使用 cfqueryparam 出于性能和安全方面的考虑,强烈建议在所有查询中使用该功能。

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

这篇关于如何在Coldfusion代码中插入单个复选框值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆