| Answer: |
If you simply do
<% For Each Item In Request.Form fieldName = Item fieldValue = Request.Form(Item) ...do something with fieldName and fieldValue... Next %>
|
you usually will not see the form fields appear in the order in which you placed them on the form. (If they do appear in order, either the form was very small or you got very lucky.)
The only reliable way to retrieve the form fields in order works like this:
<% For ix = 1 to Request.Form.Count fieldName = Request.Form.Key(ix) fieldValue = Request.Form.Item(ix) ...do something with fieldName and fieldValue... Next %>
|
This seems to work up to a certain point (at least 64 form fields, though I have heard reports of up to 256 form fields), at which point even it can break down.
If all you want is a solution, you can quit reading now. If you want to know why this happens, read on...
When you do Request.Form you are accessing a VBScript collection. And a "collection" is actually essentially the same thing as a "Scripting.Dictionary". That is, it is a hash table keyed by the names of the items in the Form.
So when you do For each item in Request.Form you are asking VBScript to traverse the KEYS of the collection, which means you will get the items in whatever order the hashing algorithm stored them in.
So the only way to predict the order is to know what hashing algorithm MS uses for the given collection. But even knowing that would only allow prediction of the order, not specifying the order.
Why does the method shown above work? Because underneath it all, the collection is really sitting on top of a "dynamic array." And when you use the code shown, you are bypassing the upper levels of the collection code and delving down to that array. And you are seeing the items in array order, which just happens to also be form field order. Even this ordering breaks down when the collection needs to "re-hash" the keys, which is why there is an upper limit (sorry I don't know for sure what it is) to the number of form fields that can be retrieved in order via the above code.
|