epipko
epipko

Reputation: 517

ColdFusion checkbox processing

I have a question about processing data for lines where checkbox is checked. When I hit "Submit", I need to end up with as many lines in a table as checked on the form.

enter image description here

Here is code that populates the form data. Checkbox''s value is unique.

<CFLOOP  query = "ship_details">
    <tr bgcolor="#IIf(CurrentRow Mod 2, DE('ffffff'), DE('f8f8f8'))#">
        <TD class="TDC" align="right"><INPUT TYPE="CHECKBOX" name="MYCHECKBOX" value="#ship_details.cust_po_nbr#" ID="chkPo" checked></TD>
        <TD class="TDC">#ship_details.cust_po_nbr#</TD>
        <TD class="TDC">#ship_details.store_id#</TD>
        <TD class="TDC">#ship_details.numb_of_cart#</TD>
        <TD class="TDC">#ship_details.total_qty#</TD>
        <TD class="TDC">#ship_details.weight#</TD>
        <TD class="TDC">#ship_details.volume#</TD>
        <cfif form.soldto EQ "AMAZON">
            <TD class="TDC"><cfinput name="pallets" type="text" size="10"></TD>
        </cfif>         
    </TR>
</CFLOOP>
        

When I hit "Submit", following query fails.

<cfquery name="abc" datasource="#REQUEST.T#">
    insert into T
    values (
      #ship_details.cust_po_nbr#
      ,#pallets#
    )
</cfquery>

It''s because values entered into fields coming over as list:

<cfloop list="#MYCHECKBOX#" index="i">
    <cfoutput>#i# - #pallets#<br /></cfoutput>
</cfloop>
<cfabort>

I see this:

152N0000 - 1,2,5

152N5IKV - 1,2,5

6N8SCRIY - 1,2,5

Upvotes: 0

Views: 115

Answers (1)

Alex
Alex

Reputation: 7833

A simple approach is appending or prepending an identifier or index to all of your input names. This way you can connect fields with each other in the controller.

Simplified example (ugly iteration)

<cfloop query="ship_details">
    <input type="checkbox" name="MYCHECKBOX_#ship_details.currentRow#" value="#ship_details.cust_po_nbr#">
    <input type="text" name="pallets_#ship_details.currentRow#">
</cfloop>

yields a form submit like

{
    "MYCHECKBOX_1": "12345",
    "MYCHECKBOX_3": "12347",
    "pallets_1": "5",
    "pallets_2": "",
    "pallets_3": "7"
}

(row 1 and 3 have been checked - unchecked inputs are not part of a submit)

which you can connect doing

<cfset inputData = []>

<cfset MAX_ROWS = 99>
<cfloop from="1" to="#MAX_ROWS#" index="row">

    <!--- has this row been checked? --->
    <cfif not structKeyExists(FORM, "MYCHECKBOX_#row#")>
        <cfcontinue>
    </cfif>

    <cfset inputData.add({
        "cust_po_nbr": FORM["MYCHECKBOX_#row#"],
        "pallets":     FORM["pallets_#row#"]
    })>

</cfloop>

<cfdump var="#inputData#">

and ending up with

[
    {
        "cust_po_nbr": "12345",
        "pallets": "5"
    },
    {
        "cust_po_nbr": "12347",
        "pallets": "7"
    }
]

Proper example

Since iterating over an artificial limit is pretty ugly, you can also just extract the checkboxes beforehand by matching against fieldnames

<cfset inputData = []>

<cfset checkedRows = reMatchNoCase("\bMYCHECKBOX_[0-9]+\b", FORM.FIELDNAMES)>
<cfloop array="#checkedRows#" index="cbKey">

    <cfset row = listLast(cbKey, "_")>

    <cfset inputData.add({
        "cust_po_nbr": FORM[cbKey],
        "pallets":     FORM["pallets_#row#"]
    })>

</cfloop>

<cfdump var="#inputData#">

Upvotes: 1

Related Questions