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

Arrays 变量中带有逗号的ColdFusion表单数组,arrays,coldfusion,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" &

当前,表单中存在复选框,并且在提交表单时,所选复选框的值存储在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>

代码继续循环遍历列表。这是我在代码中找到的对
valueList
的唯一引用。有没有一种方法可以将其转换为一个数组,而不使逗号成为问题?

我使用的模式是将逗号(,)替换为波浪号(~),因为它在我们的域中根本不使用,所以您可以使用任何您想要的字符

<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>
以下是获取所需阵列的代码:

<cfscript>
  variables.myArrayList = ListToArray(form.valueList);
  for(i=1; i LTE ArrayLen(variables.myArrayList); i=i+1)
  {
    variables.myArrayList[i] = ReplaceNoCase(variables.myArrayList[i],"~",",","all");
  } 
</cfscript>

variables.myArrayList=ListToArray(form.valueList);
对于(i=1;i LTE ArrayLen(variables.myArrayList);i=i+1)
{
variables.myArrayList[i]=ReplaceNoCase(variables.myArrayList[i],“~”,“,”,“all”);
} 
数据中不应存在分隔符(在本例中为逗号),因为这样几乎不可能识别一个元素的起始位置和另一个元素的结束位置。最好的解决方案是对复选框
值使用其他内容。例如,如果描述来自数据库表,请使用数字记录ID而不是长文本描述。那么这就不是问题了。

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

<cfset myArray = getPageContext().getRequest().getParameterValues('my_form_or_url_field_name')>

如果您的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>


你是对的,但这首先假设它来自数据库。在很多情况下,仍然需要输入一个包含逗号的列表。是的,但是您仍然可以通过指定一个临时数值来解决这个问题。然后改为使用数值。没有使用数据库ID那么优雅;)如果你不确定这些值或者它们是用户定义的,那么瓷砖也同样糟糕。我的一个同事会想出一些最可笑的定界符,比如用来处理冷音乐的愚蠢列表。
<cfset myArray = getPageContext().getRequest().getParameterValues('my_form_or_url_field_name')>
<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>