ColdFusion表单数组中带逗号的变量

csm*_*32s 3 arrays coldfusion

目前,表单中有复选框,并且在提交表单时,所选复选框的值存储在DB中.

<td><input type="Checkbox" name="valueList" value="Some value, with comma"   >Some value, with comma</td> 
<td><input type="Checkbox" name="valueList" value="Another Value, with comma"   >Another value, with comma</td> 
<td><input type="Checkbox" name="valueList" value="Yet another value"   >Yet another value</td>
Run Code Online (Sandbox Code Playgroud)

但是,问题在于逗号,因为当前逻辑使用列表来存储这些值.因此Some value, with comma插入为Some value和with comma.使用以下内容创建当前列表:

<cfif isDefined("valueList")>
<cfset a=listlen(valueList)>
Run Code Online (Sandbox Code Playgroud)

然后代码继续循环遍历列表.这是valueList我在代码中找到的唯一引用.有没有办法将此转换为数组而不会使逗号成为问题?

Rya*_*lle 9

其实有是一种方法来检索数据作为数组.如果您的enctype是application/x-www-form-urlencoded(默认的enctype),那么您只需要一行:

<cfset myArray = getPageContext().getRequest().getParameterValues('my_form_or_url_field_name')>
Run Code Online (Sandbox Code Playgroud)

如果您的enctype是multipart/form-data(这是您在上传文件时使用的类型),那么事情就会复杂一些.这是我编写的一个函数,它将返回带有给定名称作为数组的表单和url值,用于以下任一类型:

<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 tmpPartsArray = Form.getPartsArray() />
    <cfset var returnArray = arrayNew(1) /> 
    <cfset var tmpPart = 0 />
    <cfset var tmpValueArray = "" >

    <!--- if the getPartsArray method did not return NULL, then this is a multipart/form-data request, which must be handled as such. --->
    <cfif IsDefined("tmpPartsArray")>
        <cfloop array="#tmpPartsArray#" index="tmpPart">
            <cfif tmpPart.isParam() AND tmpPart.getName() EQ arguments.fieldName>
                <cfset arrayAppend(returnArray, tmpPart.getStringValue()) />
            </cfif>
        </cfloop>
    </cfif>

    <!--- Add the values that maybe on the URL with the same name, also if this *wasn't* a multipart/form-data request then
    the above code did not get any of the data, and the method below will return all of it. --->
    <cfset tmpValueArray = getPageContext().getRequest().getParameterValues(arguments.fieldName) />

    <!--- that may have returned null, so need to test for it. --->
    <cfif IsDefined("tmpValueArray")>
        <cfloop array="#tmpValueArray#" index="tmpPart">
            <cfset arrayAppend(returnArray, tmpPart) />
        </cfloop>
    </cfif>

    <cfreturn returnArray />
</cffunction>
Run Code Online (Sandbox Code Playgroud)