5

ページ 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>
4

1 に答える 1

7

フォームに表示される順序と同じ順序にする場合は、次のメカニズムを使用してループする必要があります。

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

更新: 2 番目のループは、最初のループと同じ出力を順番に提供します。質問したユーザーのリクエストにより更新。

于 2013-10-30T13:50:50.120 に答える