CFLib.org – Common Function Library Project

formFieldAsArray(fieldName)

Last updated February 20, 2013

author

Ryan Stille

Version: 2 | Requires: CF8 | Library: UtilityLib

Description:
When you pass several form or URL variables into ColdFusion with the same name, they end up as a comma separated list. This is commonly done with checkboxes – a user can check as many items as they want, then they will end up in your code all in a single variable. This works fine, until your data contains a comma. This function will return the data as an array to get around that problem. Tested on ColdFusion 8 and 9, probably runs on CF 7 also, maybe even 6. UPDATE - rewritten to work in CF10. Code is much more simple now.

Return Values:
Returns an array.

Example:

<cfset MyFormFieldAsArray = formFieldAsArray('group_of_checkboxes') />

Parameters:

Name Description Required
fieldName Name of the Form or URL field Yes

Full UDF Source:

<!---
 Returns a form field as array, useful for when you have more than one form field with the same name.
 
 @param fieldName      Name of the Form or URL field (Required)
 @return Returns an array. 
 @author Ryan Stille (ryan@stillnet.org) 
 @version 2, February 19, 2013 
--->
<cffunction name="formFieldAsArray" returntype="array" output="false" hint="Returns a Form/URL variable as an array.">
    <cfargument name="fieldName" required="true" type="string" hint="Name of the Form or URL field" />
    
    <cfset var content = getHTTPRequestData().content />
    <cfset var returnArray = arrayNew(1) />
    
    <cfloop list="#content#" delimiters="&" index="local.parameter">
        <cfif listFirst(local.parameter,"=") EQ arguments.fieldName>
            <cfif ListLen(local.parameter,"=") EQ 2>
                <cfset arrayAppend(returnArray,URLDecode(listLast(local.parameter,"="))) />
            <cfelse>
                <cfset arrayAppend(returnArray,"") />
            </cfif>
        </cfif>
    </cfloop>
    
    <cfreturn returnArray />

</cffunction>

Search CFLib.org


Latest Additions

Raymond Camden added
QueryDeleteRows
November 04, 2017

Leigh added
nullPad
May 11, 2016

Raymond Camden added
stripHTML
May 10, 2016

Kevin Cotton added
date2ExcelDate
May 05, 2016

Raymond Camden added
CapFirst
April 25, 2016

Created by Raymond Camden / Design by Justin Johnson