Janusz Chudzynski
Janusz Chudzynski

Reputation: 2710

ListToArray for submitted form

I have a web form with multiple elements with the same name for example:

<textarea name="instructions"><cfoutput>#struct.instructions# </cfoutput> </textarea>

Form is built in html/coldfusion/js and the database to store the data is MS Access.

In the script that I am using to process form submission I am using ListToArray method to get all fields with a given name.

<cfset arrayInstr = ListToArray(Form.instructions)>

Ok and here is the problem. If the submitted text contains comas ListToArray will interpret it as multiple elements.

For example if I try to submit a string "Bla bla bla bla" everything will be fine, but if string will be "bla, bla, bla" only "bla" will be submitted to database.

Is there other way to process multiple fields with the same name? I would like to avoid giving unique names. thank you, Janusz

Upvotes: 2

Views: 226

Answers (3)

nosilleg
nosilleg

Reputation: 2163

You can create an array of the individual form fields by using the getHTTPRequestData function.

<cfset Variables.RequestData = getHTTPRequestData()>
<cfset Variables.RequestContent = Variables.RequestData.Content>

<cfset Variables.FormScope = []>
<cfloop index="Variables.CurrentItem" list="#Variables.RequestContent#" delimiters="&">
    <cfset ArrayAppend(Variables.FormScope, URLDecode(ListLast(Variables.CurrentItem, "=")))>
</cfloop>

Upvotes: 0

Scott Stroz
Scott Stroz

Reputation: 7519

If you give the form field names like instructions1, instructions2, instructions3, it is easy to perform whatever logic you need in a loop.

for( var i = 1; i <=3; i++ ){
    var theValue = form["instructions" & i];

    //do whatever you need to do with theValue//
}

I think that is much easier to deal with than using JavaScript to add delimters to the form fields.

Upvotes: 6

James A Mohler
James A Mohler

Reputation: 11120

Q: Is there other way to process multiple fields with the same name?

A: Not directly

Javascript sees the items as a array. What you would need to do is have Javascript loop through all the values and create a string with a different delimiter and put that into a hidden field. The distinction between different <textarea>'s and commas would be maintained.

Upvotes: 2

Related Questions