Not yet rated

Problem

To capture MULTIPLE event code entries. I can capture SINGLE item per form, but hoping to do multiples. The record structure is: un_id, enc_no, divcode, ev_no (a MySQL auto_inc field), eventcode, modifier, text, result, i.e. 9135,1,DX,[4],BP,RightArm,120/95,NML; 9135,1,LAB,[5],VSP,Ear,'',ABN Used ColdFusion.Ajax.submitForm(args) on button submit. The form calls a CFC method:<cfset createObject("component","ptEvent").createEventCode(argumentCollection = form) /> Suggestion and solution pls. K

Solution

Use the way HTML forms work to your advantage (with proper validation) or construct dynamic form fields on the fly and extract the data for each unique record.

Detailed explanation

Two potential solutions, both of which conveniently ignore how you paint all those form fields on the screen. That part can be done with CF or Javascript, whichever meets your needs most effectively.

(1) Positional notation (dangerous)

One of the lesser-noticed features of HTML is that if you submit a form with multiple fields with the same name,

<input type="text" name="firstName" value="Bob" />

<input type="text" name="firstName" value="Joe" />

dumping out the form scope on the target page will show that form.firstName is equal to "Bob,Joe".

So, if you were to name your fields accordingly, you could do something like the following in the cfc:

<cfset var func = structNew() />
<cfset func.recordID = 1 />
<cfset func.numRecords = listLen(arguments.firstName) />

<cfloop from="1" to="#func.numRecords#" index="func.recordID">

   <cfset func.firstName = listGetAt(arguments.firstName, func.recordID) />
   <cfset func.lastName = listGetAt(arguments.lastName, func.recordID) />

   <!--- ... and so on, for each field, followed by inserting each record's worth into the db within the loop --->

</cfloop>

Big, big warning though: if you don't properly validate this form prior to submission, you will not have equal-length lists of values in every field and thus be in a world of hurt. That's why I would recommend the second approach over this one.

 

(2) A MUCH safer method - dynamic form fields

To get around the need to have equal-length lists of values (because not all fields are required), you can instead simply name the fields differently.

 

<input type="text" name="divcode_1" value="DX" />

<input type="text" name="divcode_2" value="LAB" />

 

Your CFC would then receive two arguments (divcode_1and divcode_2), or as many as you painted on the screen assuming they were all named uniquely. 

Now the fun part: you need to know how many records you're planning on inserting. Easy!  In your example (un_id, enc_no, divcode, ev_no, eventcode, modifier, text, result), you'd have 8 form fields - simply divide the number of arguments to the CFC by the number of fields per record.

<cfset var func = structNew() />

<cfset func.i = 1 />
<cfset func.numRecords = structKeyCount(arguments) / 8 />

Now we can loop from 1 to 8, building and extracting field values as needed:

<cfloop from="1" to="#func.numRecords#" index="func.i">

   <cfset func.divcode = arguments["divcode_"&func.i] />

   <!--- ... and so on for each field, followed by the insert/update statement for all values in the func scope --->

</cfloop>


+
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.

Report abuse

Related recipes