我有一個存儲名稱,電子郵件和消息的簡單表單。提交時,我將這三個字段存儲在一個二維數組中,並且每增加一次我提交表單就會將該數據添加到已提交的數據中。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> </td>
<td><input type="submit" name="submitBtn" id="submitBtn" value="SUBMIT" /></td>
</tr>
</table>
</form>
</cfoutput>
有人能指出哪裏我似乎請問錯了?
這看起來像存儲數據的風險的方法。出於各種原因,您可能會在不知情的情況下重新啓動應用程序。 –
只是在這裏觀察coupla。應用程序範圍在這裏不是正確的方法。如果網站上的多個人同時填寫表單,該怎麼辦?您應該使用會話範圍,並在表單名稱中包含一個子表單,其中包含表單完成數組。其次,對於你在CFML中做的任何事情,一個數組數組很少是正確的數據結構。你在這裏試圖做的看起來更像是一系列的結構。每個數組元素是完成的實例(即使只是表單作用域的副本)。 –