Skip to main content
Inspiring
March 26, 2014
Answered

Assign cfinput checkbox value

  • March 26, 2014
  • 3 replies
  • 8021 views

I have following code to assign value from my stored procedures.

The text cfinput works to get value from stored procedure, but check box does not get value.

The check box value return to 1, but check box does not check.

I would like to know are there any way to assign a stored procedure value to check box.

Your help and information is great appreciated,

Regards,

iccsi,

<td><cfinput type="checkbox" name="Mychk" id="Mychk" value="#MySP.MyCheckValue#"></td>

<td><cfinput type="text" name="Mytxt" id="Mytxt" value="#MySP.MyTextValue#"></td>

    This topic has been closed for replies.
    Correct answer BKBK

    iccsi wrote:

    The text cfinput works to get value from stored procedure, but check box does not get value.

    The check box value return to 1, but check box does not check.

    ...

    <cfinput type="checkbox" name="Mychk" id="Mychk" value="#MySP.MyCheckValue#">

    @Iccsi

    I hope you could find your answer amid all the distraction. The value of the checkbox, #MySP.MyCheckValue# in this case, is not what determines whether the checkbox will be checked. What ensures the checkbox will be checked is the checked attribute. That is

    <cfinput type="checkbox" name="Mychk" id="Mychk" value="#MySP.MyCheckValue#" checked>

    or

    <cfinput type="checkbox" name="Mychk" id="Mychk" value="#MySP.MyCheckValue#" checked="yes">

    3 replies

    BKBK
    Community Expert
    BKBKCommunity ExpertCorrect answer
    Community Expert
    March 31, 2014

    iccsi wrote:

    The text cfinput works to get value from stored procedure, but check box does not get value.

    The check box value return to 1, but check box does not check.

    ...

    <cfinput type="checkbox" name="Mychk" id="Mychk" value="#MySP.MyCheckValue#">

    @Iccsi

    I hope you could find your answer amid all the distraction. The value of the checkbox, #MySP.MyCheckValue# in this case, is not what determines whether the checkbox will be checked. What ensures the checkbox will be checked is the checked attribute. That is

    <cfinput type="checkbox" name="Mychk" id="Mychk" value="#MySP.MyCheckValue#" checked>

    or

    <cfinput type="checkbox" name="Mychk" id="Mychk" value="#MySP.MyCheckValue#" checked="yes">

    iccsiAuthor
    Inspiring
    March 31, 2014

    Thanks a million for the information and help,

    Regards,

    Iccsi,

    BKBK
    Community Expert
    Community Expert
    March 26, 2014

    iccsi wrote:

    I have following code to assign value from my stored procedures.

    The text cfinput works to get value from stored procedure, but check box does not get value.

    The check box value return to 1, but check box does not check.

    I would like to know are there any way to assign a stored procedure value to check box.

    ...

    <td><cfinput type="checkbox" name="Mychk" id="Mychk" value="#MySP.MyCheckValue#"></td>

    Yes, you may assign a value like that to a checkbox! But it works differently from how you expect. Run the following test, and you will notice the following main points:

    1) If you fail to tick the checkbox, ColdFusion will omit the checkbox field from the submitted form scope. No checkbox field, no value.

    2) If you assign a value to the checkbox field, which you've done here, that value will be stored in the checkbox field when the form is submitted. In view of 1), the assumption is that the checkbox is checked before submission. In your example, the value #MySP.MyCheckValue# will be stored in the variable form.Mychk.

    3) The state of the checkbox, that is, whether it is checked or not, is independent of its value attribute. The state is determined by the 'checked' attribute.

    <cfdump var="#form#">

    <cfform name="myForm" method="post">

    <cfif isdefined("form.mychk") and form.mychk is 'aaa'>

             <!--- 'Checked' is the attribute that determine the state of a checkbox, not value --->

        <cfinput type="checkbox" name="Mychk" id="Mychk" value="blah blah blah" checked="yes">

    <cfelse>

        <cfinput type="checkbox" name="Mychk" id="Mychk" value="blah blah blah">

    </cfif>

    <cfinput type="text" name="Mytxt" id="Mytxt" value="some text"><br>

    <cfinput name="sbmt" type="submit" value="send">

    </cfform>

    Dave Ferguson
    Participating Frequently
    March 26, 2014

    Any code examples using cfform are bad examples IMO.  Please use standard html forms as the cf forms give you nothing but pain and grief.

    <cfdump var="#form#">

    <cfform name="myForm" method="post">

    <cfif isdefined("form.mychk") and form.mychk is 'aaa'>

             <!--- 'Checked' is the attribute that determine the state of a checkbox, not value --->

        <cfinput type="checkbox" name="Mychk" id="Mychk" value="blah blah blah" checked="yes">

    <cfelse>

        <cfinput type="checkbox" name="Mychk" id="Mychk" value="blah blah blah">

    </cfif>

    <cfinput type="text" name="Mytxt" id="Mytxt" value="some text"><br>

    <cfinput name="sbmt" type="submit" value="send">

    </cfform>

    Also, to clarify.  An unchecked checkbox is not omitted by CF.  It is actually omitted by the browser when the form is submitted.  This is completely out of CF's control.

    1) If you fail to tick the checkbox, ColdFusion will omit the checkbox field from the submitted form scope. No checkbox field, no value.

    --Dave

    BKBK
    Community Expert
    Community Expert
    March 27, 2014

    fergusondj wrote:

    Any code examples using cfform are bad examples IMO.  Please use standard html forms as the cf forms give you nothing but pain and grief.

    As you say, that is your opinion, which you are entitled to. In my opinion, one cannot make such blanket generalizations about cfform. Yes, there are situations in which they cause grief, but there are also situations in which they are quite useful, especially to those starting out in ColdFusion.

    Also, to clarify.  An unchecked checkbox is not omitted by CF.  It is actually omitted by the browser when the form is submitted.  This is completely out of CF's control.

    You misinterpret what I said. I did not say an unchecked checkbox is omitted by ColdFusion. I will repeat what I said: "If you fail to tick the checkbox, ColdFusion will omit the checkbox field from the submitted form scope.". That is a true statement.

    Dave Ferguson
    Participating Frequently
    March 26, 2014

    First off... don't use cfinput.   Just use standard form fields. As to the checkbox being checked. Unless you set the checked attribute of the input it won't load as checked.

    <input type="checkbox" name="Mychk" id="Mychk" value="#MySP.MyCheckValue#" <cfif len(mysp.mycheckvalue)>checked="checked"</cfif>>

    <input type="text" name="Mytxt" id="Mytxt" value="#MySP.MyTextValue#">

    HTH,

    --Dave

    iccsiAuthor
    Inspiring
    March 26, 2014

    Thanks for the informaiton and help,

    I thouht that CFINPUT has more benefits and properties to use than standard controls,

    Are there any issue with CFINPUT?

    Should I not use all CFINPUT for all the forms like text, textararea, CFSELECT?

    Thanks again for helping,

    Regards,

    Iccsi,

    Dave Ferguson
    Participating Frequently
    March 26, 2014

    I would avoid using the cfinput tags all together.  They just add unncessary bloat to the ui and make things more difficult in the long run.

    --Dave