4

I have a form in Cold Fusion which contains a set of checkboxes. I am submitting this form to the page frag5.cfm. There is only one line of code in frag5.cfm.

<cfoutput>values are  #form.f1# </cfoutput>

This line shows "values are " -- that is, the form values for the checkboxes, all with name f1 are not being picked up.

Is this the wrong way to pick up these values? Could someone tell me the right thing to do? The original form is below:

<form name      = "fields"
  action    = "frag5.cfm"
  method    = "post"       
  onkeypress = "return event.keyCode != 13;">  
<table style = "margin-left:165px" >
<caption style = "padding-top: 20px" >Restrict Report To: </caption>
<tr>
<td>Master Event:</td>
 <td><input type = "checkbox" 
            id   = "cb_e"
            name = "f1" 
            value = "" 
            onclick = ischecked("cb_e","hid_e")
            class = "check1">
 <label for = "cb_e">&nbsp; </label>
 <input type = "hidden" name = "finsel" id = "hid_e" value = "unchecked">
</td> 
<td style = "width: 25px"> &nbsp; </td>
<td>SubEvent:</td>
  <td><input type = "checkbox" 
             id = "cb_s" 
             value = "" 
             name = "f1"
            onclick = ischecked("cb_s","hid_s")                
            class = "check1" >
  <label for = "cb_s">&nbsp; </label></td>
   <input type = "hidden" name = "finsel" id = "hid_s" value = "unchecked">
  </td>     
  </tr>
</table>
<table style ="margin-left: 175px; margin-top: 20px;" >
<td style = "width: 150px;" id = "chuprep" class = ixes2">&nbsp;       
</td>
<td style = "width: 150px;"> 
 <input type = "Submit"
        name  ="Submitfin1" 
        class = "submitbut"            
        value = "Submit" > 
</td>
</tr>
</table> 
</form>

The code for ischecked is :

function ischecked(id, passid) {
//alert("got to ischecked" + id + passid);

var ischeck = document.getElementById(id).checked; 

if(ischeck){
document.getElementById(passid).value = "checked";}
else {
document.getElementById(passid).value = "unchecked";}

}//end function ischeck
6
  • Can you add the code for ischecked(..,..) ?
    – blurfus
    Oct 16, 2015 at 22:18
  • I've put the ischecked code there. It is the workaround to the fact that I'm not picking up the checkbox values. I'm using the hidden input instead, which is going through properly. Would prefer to abandon this workaround if possible.
    – Betty Mock
    Oct 16, 2015 at 22:47
  • Might want to rethink your approach here. As @linuxdan said, the values will only be submitted if the boxes are checked. Why not just assign the box values "MasterEvent" and "SubEvent". Then use list functions to take a specific action based on which values were submitted? Obviously, verify the checkbox field exists first OR use <cfparam> to set a default value if neither was checked.
    – Leigh
    Oct 17, 2015 at 0:54
  • Yes, I understand the values are submitted only if a box is checked, and I assure you I checked a box before getting my blank answer. I don't quite understand about assigning the box values -- I thought a checkbox could only have values true or false.
    – Betty Mock
    Oct 17, 2015 at 2:35
  • 3
    No, it can have any value. Perhaps that is the issue, your boxes have an empty string for the "value". Instead, assign the values "MasterEvent" and "SubEvent" (and get rid of the hidden fields and JS) and it works just fine.
    – Leigh
    Oct 17, 2015 at 15:36

2 Answers 2

5

If you are trying to check the values of unchecked checkboxes I don't think they will show up in the FORM scope. If the checkboxes are checked, then they should show up. You can use structKeyExists(FORM, 'f1') to check if a value exists.

3
  • Good catch. Only checked boxes are submitted.
    – Leigh
    Oct 16, 2015 at 23:52
  • Well, I wouldn't try to check the value of unchecked boxes -- that's sort of a contradiction isn't it? The problem was I was picking up the values of the checked ones, but I had set those values to " ", so nothing was coming through. Leigh's earlier comment solved the problem.
    – Betty Mock
    Oct 17, 2015 at 23:50
  • RE: nothing was coming through Technically, something was passed - an empty string "", which turned out to be the problem. The checkboxes were working correctly, they just had the wrong "values". RE: that's sort of a contradiction isn't it Unexpected, yes, but honestly storing a value for unchecked boxes is a bit unusual as well, so it was a reasonable possibility to raise. When answering questions, folks do not know anything about the background of the asker, so they tend not to make too many assumptions.
    – Leigh
    Oct 18, 2015 at 14:29
0

Once thing to always do is to put quotes around your attribute values. They are not mandatory but they save you from issues like this one.

You didn't have any quotes ' (single or double) around onclick so the JS was not executing.

Adding those (and also modified your function a bit for confirmation) and I think it all works now. Can you try it?

function ischecked(id, passid){
    var ischeck = document.getElementById(id);
    console.log(ischeck.checked);

    if (ischeck.checked) {
      document.getElementById(passid).value = "checked";
      console.log( 'setting value to checked');
    } else {
      document.getElementById(passid).value = "unchecked";
      console.log( 'setting value to unchecked');
    }
  
  console.log( document.getElementById(passid).value );
    
  } //end function ischeck
<form name="fields" action="frag5.cfm" method="post" onkeypress="return event.keyCode != 13;">
  <table style="margin-left:165px">
    <caption style="padding-top: 20px">Restrict Report To:</caption>
    <tr>
      <td>Master Event:</td>
      <td>
        <input type="checkbox" id="cb_e" name="f1" value="" onclick='ischecked( "cb_e", "hid_e")' class="check1">
        <label for="cb_e">&nbsp;</label>
        <input type="hidden" name="finsel" id="hid_e" value="unchecked">
      </td>
      <td style="width: 25px">&nbsp;</td>
      <td>SubEvent:</td>
      <td>
        <input type="checkbox" id="cb_s" value="" name="f1" onclick='ischecked( "cb_s", "hid_s")' class="check1">
        <label for="cb_s">&nbsp;</label>
      </td>
      <input type="hidden" name="finsel" id="hid_s" value="unchecked">
      </td>
    </tr>
  </table>
  <table style="margin-left: 175px; margin-top: 20px;">
    <td style="width: 150px;" id="chuprep" class=i xes2 ">&nbsp;       
</td>
<td style = "width: 150px; "> 
 <input type = "Submit "
        name  ="Submitfin1 " 
        class = "submitbut "            
        value = "Submit " > 
</td>
</tr>
</table> 
</form>

3
  • Hi Ochi, You are right, there are no quotes around the JS function. I have thousands of lines of code and have used the quotes everywhere else that I should, and somehow I left these out, not to mention using double quotes around the arguments. Yet, strangely, the JS is running fine. A mystery of the interpreting process. If I had any free time, which I don't, I would fiddle around with this, to try to understand better why it is working.
    – Betty Mock
    Oct 18, 2015 at 0:03
  • Ochi-- could you tell me how to look at the console log -- I was unaware of this feature, and would like to use it in the future. BTW, I didn't fool around with the unchecked items in the JS, since they are unchecked by default in the hidden inputs.
    – Betty Mock
    Oct 18, 2015 at 0:05
  • Depending on the browser you are using, F12 works for Firefox and Chrome (on windows, I believe)
    – blurfus
    Oct 19, 2015 at 16:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.