2015-09-19 22 views
1

我有一個存儲名稱,電子郵件和消息的簡單表單。提交時,我將這三個字段存儲在一個二維數組中,並且每增加一次我提交表單就會將該數據添加到已提交的數據中。ColdFusion數組 - 在表中存儲表單數據

但是,每次我提交表單而不是在數組中創建一個新元素時,它都會覆蓋以前提交的內容。

在我的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> 

有人能指出哪裏我似乎請問錯了?

+2

這看起來像存儲數據的風險的方法。出於各種原因,您可能會在不知情的情況下重新啓動應用程序。 –

+1

只是在這裏觀察coupla。應用程序範圍在這裏不是正確的方法。如果網站上的多個人同時填寫表單,該怎麼辦?您應該使用會話範圍,並在表單名稱中包含一個子表單,其中包含表單完成數組。其次,對於你在CFML中做的任何事情,一個數組數組很少是正確的數據結構。你在這裏試圖做的看起來更像是一系列的結構。每個數組元素是完成的實例(即使只是表單作用域的副本)。 –

回答

0

您的代碼適用於我。我懷疑您的每個請求都會觸發您的onApplicationStart事件。

,以確保它不是你Application.formSubmissions的重新定義,刪除行<cfset Application.formSubmissions = ArrayNew(2) />onApplicationStart功能,並與下面的代碼替換你的榜樣的<cfscript>部分:

<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偷懶的方法並阻止娛樂。 注意:這僅用於測試目的,因爲應用程序範圍應被鎖定以避免競爭條件。無論您應該添加表單驗證。

0

使用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> 
0

一個提示:

使用CompareNoCase比較字符串。它比EQ/NEQ更快。請參閱下面的信息。

CompareNoCase

說明:執行兩個字符串的區分大小寫的比較。

返回:差的指標:

負數,如果string1小於字符串2 0,如果字符串2是等於字符串2爲正數,如果字符串2是大於字符串2

功能語法:

CompareNoCase(Form.name, "") 

如果以上返回0,表單變量爲空。

更多datails在ColdFusion Documentation