Question

I am using Coldfusion MX7 and have a basic form which can have several elements that are dynamically added to the form. They are given the same name and are all checkboxes. An example of the form is as follows:

<form action="index.cfm?action=index.report" method="post" id="reportForm">
<div class="report my">
    <ul class="connectWith ui-sortable" id="fieldListSelect" aria-disabled="false">
        <li class="field" id="field_profileFn" style="">
            <a class="action" id="action_profileFn" href="index.cfm?action=index.filter.profileFn" style="display: block; ">filter</a> 
            <label for="profileFn">First Name</label>
            <input type="checkbox" name="reportItem" id="profileFn" value="profileFn">
        </li>
        <li class="field" id="field_profileSn" style="">
            <a class="action" id="action_profileSn" href="index.cfm?action=index.filter.profileSn" style="display: block; ">filter</a> 
            <label for="profileSn">Surname</label>
            <input type="checkbox" name="reportItem" id="profileSn" value="profileSn">
        </li>
        <li class="field" id="field_contactDate" style="">
            <a class="action" id="action_contactDate" href="index.cfm?action=index.filter.contactDate" style="display: block; ">filter</a> 
            <label for="contactDate">Contact date</label>
            <input type="checkbox" name="reportItem" id="contactDate" value="contactDate">
        </li>
    </ul>
</div>
</form>

Once the form is posted I get the following through cfdump:

<table class="cfdump_struct">
    <tr><th class="struct" colspan="2" onClick="cfdump_toggleTable(this);" style="cursor:hand;" title="click to collapse">struct</th></tr>

        <tr><td class="struct" onClick="cfdump_toggleRow(this);" style="cursor:hand;" title="click to collapse">CONTACTDATE_FROM</td>
        <td>  Thu May 19 2011 00:00:00 GMT+0100 (GMT Daylight Time) </td></tr> 
        <tr><td class="struct" onClick="cfdump_toggleRow(this);" style="cursor:hand;" title="click to collapse">CONTACTDATE_TO</td>
        <td> Thu May 19 2011 00:00:00 GMT+0100 (GMT Daylight Time) </td></tr> 
        <tr><td class="struct" onClick="cfdump_toggleRow(this);" style="cursor:hand;" title="click to collapse">FIELDNAMES</td>
        <td> REPORTITEM[],CONTACTDATE_FROM,CONTACTDATE_TO </td></tr> 
        <tr><td class="struct" onClick="cfdump_toggleRow(this);" style="cursor:hand;" title="click to collapse">REPORTITEM[]</td>
        <td> profileFn,profileSn,contactDate </td></tr> 
    </table>

The element REPORTITEM[] is reported and in trying to access this as a variable I get:

<cfset testing = form.reportItem[]>

Invalid CFML construct found on line 6 at column 50.

In trying to access the variable in the way I would expect I get the following:

<cfset testing = form.reportItem>

Element REPORTITEM is undefined in FORM.

I have inherited this code and it MUST have worked previously. Coldfusion has not been upgraded (obviously being CF 7 still) and nothing else has changed server side that I can think of.

My questions:

  • Is this just a limitation of CF7?
  • This should work right or is this totally wrong?
  • I am going to have to re-write quite a bit of this code if this just doesn't work, handling this after the data has been posted would be easier to code. Modifying the form will be more effort, so is it possible?
Was it helpful?

Solution

Try doing

<cfset testing = form["reportItem[]"]>

This will fetch the form struct by the key "reportItem[]".

OTHER TIPS

As far as I know, CF7 has no problem with this. In fact, I'm pretty sure that the value of your checkboxes is constructed by the browser, not the webserver or CF.

Here's what I see:

form.variableNamve[]

will not work, because the value is coming back as a comma-delimited list.

You will run into the not defined error if no checkboxes are checked, because if no checkboxes with that name are checked then that variable will not be submitted by the browser, and therefore will not exist in the form scope. You should default this, and there are a couple of ways to do it.

You can create a new struct with the checkbox name as a key, the empty string as the value, then structAppend the form scope on top of it.

You can use the traditional cfparam tag.

You can add a hidden form field with the same name and the empty string as a value to the form. This forces the browser to return the form field, even if no checkboxes are checked.

HTH.

Are you posting through jQuery ajax or using normal submit button. I think jQuery add variablename[] while posting it but there is way to disable it. But in case of submit button I will get checkbox only in form structure only if atleast one checkbox is checked. In this case always cfparam checkbox name with your default value.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top