coldfusion - 从表单中按顺序显示 CFLoop 项目

标签 coldfusion coldfusion-9

我在页面 form.html 上有以下表单,它提交给 cfpage.cfm。名字、姓氏、地址和年龄都显示出来,但顺序不一致。有时它会显示姓氏、名字、地址和年龄。在另一种情况下,它可能会显示地址、名字、年龄,然后是姓氏。

如何显示 CFLoop 项目 - 用户在文本框中输入的文本 - 按照它们在表单中的显示顺序?我有多个通用表单,所以我必须在 cfpage.cfm 上使用一些通用代码来捕获提交的任何馈送表单。

<form id="theform" name="theform" action="cfpage.cfm" method="post">
First Name
<input type="text" name="first name">

Last Name
<input type="text" name="last name">

 Address
<input type="text" name="address">

Age
<input type="text" name="age">
</form>

cfpage.cfm 上的代码
<cfloop collection="#form#" item="theField">
<cfif theField is not "fieldNames">
#theField# = #form[theField]#<br>
</cfif>
</cfloop>

最佳答案

如果您希望它们按照它们出现在表单上的相同顺序,那么您必须使用以下机制进行循环:

<cfloop index="i" list="#Form.FieldNames#" delimiters=",">
    #Form[i]#
</cfloop>

下面是验证您所看到的问题的代码,并显示上述循环有效——另存为 stacktest.cfm:
<form id="theform" name="theform" action="stacktest.cfm" method="post">
First Name <input type="text" name="first name">
Last Name <input type="text" name="last name">
Address <input type="text" name="address">
Age <input type="text" name="age">
<input type="submit" value="submit"/>
</form>

<cfoutput>
<cfloop collection="#form#" item="theField">
<cfif theField is not "fieldNames">
    #theField# = #form[theField]#<br>
</cfif>
</cfloop>

<cfloop index="i" list="#Form.FieldNames#" delimiters=",">
    #i# = #Form[i]#<br>
</cfloop>
</cfoutput>

更新:现在,第二个循环提供与第一个循环相同的输出,只是按顺序排列。根据提出问题的用户的要求更新。

关于coldfusion - 从表单中按顺序显示 CFLoop 项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19683868/

相关文章:

javascript - 将 curl 查询转换为 jQuery.ajax()

java - 如何在不重新启动服务的情况下重新加载 Coldfusion 中的打印机?

oauth - 如何在 ColdFusion 9 中签署 Quickbooks 在线 API 请求?

coldfusion - 如何将 cftimer 显示的时间存储在变量中?

html - 圆形元素符号未显示在 html 页面中

coldfusion - 如何部署 ColdFusion 本地开发环境?

sql - 如何从查询中删除特定列 ColdFusion 11

java - 为什么 cflock 有时会导致 'timeout value is negative' 错误?

coldfusion - 是否有必要在CFScript中更改作用域循环变量?

sql - ColdFusion 表单不向数据库提交数据