Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic form processing and Random Output display

Status
Not open for further replies.

evie71

MIS
Nov 14, 2002
6
GB
Hi,
I am working with a dynamic form action page that creates an array of all formfield names and their corresponding values, then loops and outputs them into an email. The form is a basic HTML form, with hidden fields to define the TO, CC etc fields within CFMAIL.

The problem I have noticed, is that every email displays the field names and values in a different and totally random order. I would expect the output of field/values to be either in the order they appear in the HTML form, OR to at least list them alphabetically - but everytime the email is sent, the fields appear in a different order!!

The form action code:

<!--- Define Arrays to get the Field names and values --->
<cfset fieldArray = ArrayNew(1)>
<cfset valueArray = ArrayNew(1)>
<cfset testArray = ArrayNew(2)>

<!--- Open a loop to collect all the Form fields and Value --->
<cfloop collection=&quot;#form#&quot; item=&quot;i&quot;>

<!--- Ignore Known field names and values --->
<cfif #i# is &quot;EMAIL_TO&quot; OR #i# is &quot;SUBMIT&quot; OR #i# is &quot;FIELDNAMES&quot; OR #i# is &quot;RESET&quot;
OR #i# is &quot;CC_TO&quot; OR #i# is &quot;SUBJECT&quot; OR #i# is &quot;MYURL&quot; or #i# is &quot;ON_SUCCESS&quot;
OR #i# is &quot;ON_ERROR&quot;>
<cfset dummy=&quot;#i#&quot;>
<cfelse>
<!--- Add the E-mailable Field names and Values to respective Arrays --->
<cfset ArrayAppend(fieldArray,&quot;#i#&quot;) >
<cfset ArrayAppend(valueArray,&quot;#form#&quot;) >
</cfif>
</cfloop>

<!--- Generate the e-mail --->
<cfset to_element=#ArrayLen(fieldArray)#>
<cftry>
<cfmail to=&quot;#email_to#&quot;
from=&quot;Xtranet@xtranet.com&quot;
subject=&quot;#subject#&quot;
cc=&quot;#cc_to#,#email_from#&quot;
type=&quot;HTML&quot;>

This mail comes to you from the URL : #myurl#

<cfloop index=&quot;disList&quot; from=&quot;1&quot; to=#to_element# >
<p>#fieldArray[&quot;#dislist#&quot;]# : <strong>#valueArray[&quot;#disList#&quot;]#</strong></p>
</cfloop>
</cfmail>

<cflocation url = &quot;#on_success#&quot; >

<cfcatch type=&quot;Any&quot;>
<cflocation url = &quot;#on_error#&quot; >
</cfcatch>
</cftry>

Can anyone suggest a way in which we can set the order in which they are either entered into the array or outputted into the email that is consistent?

Thanks in advance
Eve
 
Can anyone point me in the right direction with this query please? Although it's not desperately urgent, I would like to understand how and in what order CF processes form variables and how I might be able to control this better..

Any pointers would be appreciated.
Thanks in advance
E
 
How about just ditching the array idea altogether and using the FORM scope as a structure (as you are doing, but without putting all of the values into an array). From what I can tell with some simple tests (at least in CF 5.0), formfields are sent to the post page in alphabetical order, however, you can use the StructSort() on the FORM scope just in case.

Code:
<!--- Sort the form structure, just in case! --->
<cfset rs = StructSort(FORM)>

<!--- Create a list to hold non-essential formfields that would otherwise appear in the body of the email message --->
<cfset KillList = &quot;EMAIL_TO,SUBMIT,FIELDNAMES,RESET,CC_TO,SUBJECT,MYURL,ON_SUCCESS,ON_ERROR&quot;>

<!--- Generate the e-mail --->
<cfset to_element=#ArrayLen(fieldArray)#>
<cftry>
 <cfmail to=&quot;#email_to#&quot;
        from=&quot;Xtranet@xtranet.com&quot;
        subject=&quot;#subject#&quot;
        cc=&quot;#cc_to#,#email_from#&quot;
        type=&quot;HTML&quot;>

     This mail comes to you from the URL : #myurl#

    <cfloop collection=&quot;#FORM#&quot; item=&quot;ThisField&quot;>
		<!--- If we don't find the current formfield in the KillList, output it and it's value --->
		<cfif ListFindNoCase(KillList,ThisField) EQ 0>
		<p>#ThisField# : <strong>#FORM[ThisField]#</p>
		</cfif>
	</cfloop>	
   </cfmail>
  
   <cflocation url = &quot;#on_success#&quot; >

   <cfcatch type=&quot;Any&quot;>
       <cflocation url = &quot;#on_error#&quot; > 
   </cfcatch>
</cftry>

HTH,

-Tek
 
Dear Tek,
Thanks for your reply - I have tried what you suggested, getting rid of the array BUT I get exactly the same results!!

The form fields listed in the email are outputted and displayed in a random and totally different order EACH time!
I need to find a way of consistently displaying the form fields either in the order they appear in the form, or in alphabetical order - but either way it needs to be the same output each time.

In my example, I have the following form fields (in this order): NAME, EXT, EMAIL, LOCATION, DIVISION, FEEDBACK

In 3 tests with the same data the output is displayed in email as follows:
1) NAME: LOCATION: EXT NO: DIVISION: FEEDBACK: EMAIL_FROM:
2) LOCATION: DIVISION: EXT NO: FEEDBACK: NAME: EMAIL_FROM:
3) NAME: DIVISION: FEEBACK: LOCATION: EMAIL_FROM: EXT NO:

Does anyone have anymore suggestions please. We are using CF MX.

thanks
E
 
Hi Evie, here is the revised code:

<!--- Sort the FORM.FIELDNAMES list --->
<cfset ListSort(FORM.FIELDNAMES,&quot;textnocase&quot;,&quot;asc&quot;)>

<!--- Create a list to hold non-essential formfields that would otherwise appear in the body of the email message --->
<cfset KillList = &quot;EMAIL_TO,SUBMIT,FIELDNAMES,RESET,CC_TO,SUBJECT,MYURL,ON_SUCCESS,ON_ERROR&quot;>

<!--- Generate the e-mail --->
<cfset to_element=#ArrayLen(fieldArray)#>
<cftry>
<cfmail to=&quot;#email_to#&quot;
from=&quot;Xtranet@xtranet.com&quot;
subject=&quot;#subject#&quot;
cc=&quot;#cc_to#,#email_from#&quot;
type=&quot;HTML&quot;>

This mail comes to you from the URL : #myurl#

<cfloop list=&quot;#FORM.FIELDNAMES#&quot; index=&quot;ThisField&quot;>
<cfif ListFindNoCase(KillList,ThisField) EQ 0>
<p>#ThisField# : <strong>#FORM[ThisField]#</p>
</cfif>
</cfloop>

</cfmail>

<cflocation url = &quot;#on_success#&quot; >

<cfcatch type=&quot;Any&quot;>
<cflocation url = &quot;#on_error#&quot; >
</cfcatch>
</cftry>

The previous method I showed you was wrong! I hadn't used StructSort() in so long that I totally botched it. The way I presented it here works (I tested the logic on a dummy page I set up). This is pretty straightforward, and it could be done in a few different ways, such as using an array, but speed-wise there was no difference when I was playing around with that.

HTH!

-Tek
 
Dear Tek,
Thank you SO much!!
Sorry for not getting back to you sooner, I have been out on a course.

Yes it works.. just how it should!
Thanks again, and have a great Christmas

Eve
 
I think the order of the <cflocation tags should be reversed.
It is my understanding that you want to catch any error first and if it gets past that your fine.


.
.
.

<cfcatch type=&quot;Any&quot;>
<cflocation url = &quot;#on_error#&quot; >
</cfcatch>

<cflocation url = &quot;#on_success#&quot; >
</cftry>

Aaron




 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top