【问题标题】:ColdFusion Form Array with comma in variable变量中带有逗号的 ColdFusion 表单数组
【发布时间】:2023-04-07 10:03:01
【问题描述】:

目前,表单中有复选框,并且在提交表单时将选中的复选框的值存储在数据库中。

<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>

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

<cfif isDefined("valueList")>
<cfset a=listlen(valueList)>

然后代码继续循环遍历列表。这是我在代码中可以找到的对valueList 的唯一引用。有没有办法将其转换为数组而不会出现逗号问题?

【问题讨论】:

    标签: arrays coldfusion


    【解决方案1】:

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

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

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

    <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>
    

    【讨论】:

      【解决方案2】:

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

      <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>
      

      所以当表格过来的时候,它会是这样的:

      form.valueList = "Some value~ with comma, Another Value~ with comma, Yet another value";
      

      这是获取你想要的数组的代码:

      <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>
      

      【讨论】:

      • 如果您不确定这些值或者它们是用户定义的,那么波浪号同样糟糕。我的一位同事会想出像 这样最荒谬的分隔符来处理 ColdFusion 的愚蠢列表。
      【解决方案3】:

      分隔符(在本例中为逗号)不应出现在数据中,因为这样几乎不可能识别一个元素的开始位置和另一个元素的结束位置。最好的解决方案是为复选框value 使用其他东西。例如,如果描述来自数据库表,则使用数字记录 ID 而不是长文本描述。那么这将不是问题。

      【讨论】:

      • 你是对的,但这是假设它首先来自数据库。仍然有很多情况需要输入包含逗号的事物列表。
      • 是的,但是您仍然可以通过分配一个临时数值来解决它。然后改用数值。不像使用数据库 ID 那样优雅;)但类似的概念。
      猜你喜欢
      • 1970-01-01
      • 2018-07-12
      • 1970-01-01
      • 2018-03-17
      • 2018-05-27
      • 2023-04-01
      • 2022-11-02
      • 1970-01-01
      • 2012-07-25
      相关资源
      最近更新 更多