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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Ordering of form elements 2

Status
Not open for further replies.

FateFirst

Programmer
Apr 15, 2002
212
0
0
GB
Hi.

A friend wanted a script that would email him the details of a form he creates. As he creates many different booking forms he wanted a script that he could just point his forms at and it would do the rest....so he didnt have to keep bothering me.

So i just wrote quick script that did so.

All i did was a very simple:

Code:
	For each item in request.form
		Generate Body Email
	Next
        
        <INSERT EMAIL SCRIPT>

This works fine in handling any form he directs at this page but the order in which it comes through is different to the order that the form is setup as.

Example;

The form goes:

NAME
ADDRESS1
ADDRESS2
BLAH BLAH
INVOICE ADDRESS1
INVOICE ADDRESS2

But the email data comes through

INVOICE ADDRESS1
INVOICE ADDRESS2
NAME
ADDRESS1
ADDRESS2
BLAH BLAH

Is this something that can be controlled at the form end by specifying something in any way or does this level of control require proper handling of the form data within my script?

Any help would be appreciated.

Thanks! :D

- FateFirst
 
I do not think there is a way to control the order in which elements appear in the form object though if there is someone will probably come in and say so.

I see two possible methods for doing it through code but both involve modification at the form end.
The first would be to name all of the form fields in a predictable incremental manner so that you can loop through them in your ASP. For instance add Fxx to the beginning of each field name where xx is an incremental value. This way you can specifiy the order of fields for the ASP page. F00, F01, F02, etc. The F just being any non-numerical character to start the name with.
In your asp page you would have to loop through the items in the form object and check to see if their name begins with F and the number you are looking for.

Another method could be to have a hidden form field that contains a list of the field names in the order you want them presented. Read that field, parse out the names and use that info to dynamically read in the fields in the order you want to display them.

If someone else does not have a better approach then the above might be useful.


Stamp out, eliminate and abolish redundancy!
 
The problem is almost certainly in the 'Generate Body Email' section - please can you paste the code into the thread.

cheers
 
Ooh, just found this, it should provide the fields in order.
Code:
<%
For ix = 1 to Request.Form.Count
    fieldName = Request.Form.Key(ix)
    fieldValue = Request.Form.Item(ix) 
    ...do something with fieldName and fieldValue...
Next
%>

Found it at:
Stamp out, eliminate and abolish redundancy!
 
There is a variable key for the order of posted form values, where i is your loop iteration variable

the following gives you the 'name' of the form field
request.form.key(i)

and this gives you the form field value
request.form.item(i)

-DNG
 
Thanks niteowl/DotNetGnat. I implemented your find/info into my script and it solved my problem.

Which shows that the problem was certainly NOT in the 'Generate Body Email', emozley :p

Once again thanks guys!

- FateFirst
 
Not be a joy kill, but do not be surprised if you get your fields out of order again at some point in the future. Browsers are not required to post the form values in any particular order and, in fact, it says as much in the standards.

barcode_1.gif
 
The only way to be certain that you can always retrieve form elements in a predictable order is to give them names that imply some order.

For Example: Thing1, Thing2, Thing3, etc...

You might alway want some MaxThing.
 
I have an ASP program that display server variables sorted alphabetically. You could use the same technique to sort your form variables. It uses some quicksort code that I believe I picked up here (thanks).

The program:

The program in text form:

The quicksort include file:

If your browser tries to display the HTML in the .txt file (IE does), just View Source and copy the code.

Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
It sounds as if tsdragon's approach may ultimately prove the most reliable for a server-side approach, you just have to be certain that the field names are named so that they will sort into the correct order. Remember never to start a field name with a number.

Myself, I always format the output email prior to submitting the form so that I have an email with full formatting as it was presented to the client originally making it easier to read. But this is for cases when the only output is to an email message and the data is not be automatically handled for other purposes.


Stamp out, eliminate and abolish redundancy!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top