【问题标题】:ColdFusion Array - storing form data in arrayColdFusion Array - 将表单数据存储在数组中
【发布时间】:2015-09-19 11:52:05
【问题描述】:

我有一个简单的表单,用于存储姓名、电子邮件和消息。提交后,我将 3 个字段存储在 2D 数组中,并且每次提交表单时,我都想将该数据添加到已提交的内容中。

但是,每次我提交表单时,它都不会在数组中创建一个新元素,而是会覆盖之前提交的内容。

在我的 Application.cfc 中,我声明我的数组如下:

<cffunction name="onApplicationStart">
    <!--- Define array that will store form submissions --->
    <cfset Application.formSubmissions = ArrayNew(2) />
</cffunction>

而我的 test.cfm 页面有主要代码:

<cfscript>
// if form submitted
if (StructKeyExists(Form,"name") AND Form.name NEQ "") {
    // define constants for column names
    Variables.name = 1;
    Variables.email = 2;
    Variables.message = 3;
    // define array position
    Variables.arrayLen = ArrayLen(Application.formSubmissions);
    Variables.arrayPos = Variables.arrayLen + 1;
    // add form data to array
    Application.formSubmissions[Variables.arrayPos][Variables.name] = Form.name;
    Application.formSubmissions[Variables.arrayPos][Variables.email] = Form.email;
    Application.formSubmissions[Variables.arrayPos][Variables.message] = Form.message;
    // reset form fields to stop insert
    Form.name = "";
    Form.email = "";
    Form.message = "";
}
</cfscript>
<cfdump var="#Application.formSubmissions#">
<cfoutput>

<form action="##" method="post" name="contactForm" id="contactForm">
    <table>
        <tr>
            <td><label name="nameLabel" for="name">Name</label></td>
            <td><input type="text" name="name" id="name" /></td>
        </tr>
        <tr>
            <td><label name="emailLabel" for="email">Email</label></td>
            <td><input type="text" name="email" id="email" /></td>
        </tr>
        <tr>
            <td><label name="emailLabel" for="email">Message</label></td>
            <td><textarea name="message" id="message" cols="50" rows="5"></textarea></td>
        </tr>
        <tr>
            <td>&nbsp;</td>
            <td><input type="submit" name="submitBtn" id="submitBtn" value="SUBMIT" /></td>
        </tr>
    </table>

</form>

</cfoutput>

谁能指出我哪里出错了?

【问题讨论】:

  • 这看起来像是一种危险的数据存储方式。由于各种原因,应用程序可能会在您不知情的情况下重新启动。
  • 这里只是一个耦合观察。应用范围在这里不是正确的方法。如果网站上不止一个人同时填写表格怎么办?您可能应该使用会话范围,并在该范围内有一个用于表单名称的子键,其中包含一个表单完成数组。其次,对于您将在 CFML 中执行的任何操作,数组数组很少是正确的数据结构。你在这里尝试做的看起来更像是一个结构数组。每个数组元素都是完成的实例(即使只是表单范围的副本也可以)。

标签: arrays forms struct coldfusion


【解决方案1】:

您的代码对我有用。我怀疑您的 onApplicationStart 事件会在每个请求时触发。

为确保它不是您的 Application.formSubmissions 的重新定义,请删除您的 onApplicationStart 函数中的 &lt;cfset Application.formSubmissions = ArrayNew(2) /&gt; 行,并将示例中的 &lt;cfscript&gt; 部分替换为以下代码:

<cfscript>

    if ( !structIsEmpty(Form) ) {

        if (
            ( structKeyExists(Form, "name") && len(trim(Form.name)) ) &&
            ( structKeyExists(Form, "email") && len(trim(Form.email)) ) &&
            ( structKeyExists(Form, "name") && len(trim(Form.name)) )
        ) {

            if ( !structKeyExists(Application, "formSubmissions") ) {
                Application.formSubmissions = [];
            }

            Application.formSubmissions.add([
                trim(Form.name),
                trim(Form.email),
                trim(Form.message)
            ]);
        }

    }

</cfscript>

这将以惰性方式初始化Application.formSubmissions 并阻止重新创建。 注意:这仅用于测试目的,因为应锁定应用程序范围以避免竞争条件。无论如何,您都应该添加表单验证。

【讨论】:

    【解决方案2】:

    使用struct,更好更容易理解。

    <cffunction name="onApplicationStart">
        <!--- Define array that will store form submissions --->
        <cfset Application.formSubmissions = ArrayNew(1) />
    </cffunction>
    <cfscript>
        Application.formSubmissions[Variables.arrayPos] = StrunctNew();
        Application.formSubmissions[Variables.arrayPos].name = Form.name;
        Application.formSubmissions[Variables.arrayPos].email  = Form.email;
        Application.formSubmissions[Variables.arrayPos].message  = Form.message;
    </cfscript>
    

    【讨论】:

      【解决方案3】:

      另一个提示:

      使用 CompareNoCase 比较字符串。它比 EQ/NEQ 更快。请参阅以下信息。

      比较NoCase

      说明:对两个字符串进行不区分大小写的比较。

      返回:差异指标:

      负数,如果string1小于string2 0,如果string1等于string2 正数,如果string1大于string2

      函数语法:

      CompareNoCase(Form.name, "")
      

      如果上面返回0,则表单变量为空。

      更多数据在ColdFusion Documentation

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-01-13
        • 1970-01-01
        • 2012-04-23
        • 2011-02-24
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多