...

View Full Version : Order of fields posted?



BigDaddy
10-10-2003, 08:33 PM
Suppose a guy had a form with 10 fields. Is there any way that you can set the order in which they are posted to the next page? For instance, do a

Dim Item
For Each Item in Request.form
Response.Write(Item & " = " & Request.form(Item) & "<br />" & vbCrLf)
Next
response.end

And it's going to loop through the request.form collection and write them out the page. Is there anyway that you can control the order in which they are posted to the request.form collection, so you don't have stuff appearing like:

q1
q2
q3
q10
q8
q4
q5

...

See, questions 10 and 8 are out of place.

raf
10-11-2003, 07:43 PM
Hmm. Not sure. Posting the form is a clientside action and i don't know if al browsers proces the form the same way.

Not sure i understand it, but if you name the formfields like q1, q2 , ... q10 , then you can proces them in that order (or any order you want). Like


Dim i, item
i = 1
do while i <= 10
item = "q" & i
Response.Write(item & " = " & Request.form(item) & "<br />" & vbCrLf)
i = i + 1
loop
response.end

ReyN
10-12-2003, 01:24 AM
hmmm, not sure, but it would most probably be in the same order as how the fields are sequentially accessed in the form itself. :rolleyes:

http://authors.aspalliance.com/aspxtreme/sys/Web/HttpRequestClassForm.aspx

whammy
10-13-2003, 03:00 AM
for i = 1 to Request.Form.Count
for each name in Request.Form
if Left(name, 1) <> "_" and _
Request.Form(name) is Request.Form(i) then
if str <> "" then
str = str & ","
end if
str = str & name
exit for
end if
next
next

from brainjar's website (http://www.brainjar.com), I use very similar code, that was the quickest example I could find.

BigDaddy
10-13-2003, 03:06 PM
The reason I'm asking, is that a friend has built a site with one of those free hosting companies that give you a cgi process or something like that for him to post it to. It then mails him the results.

He gets them out of order: Q1, Q10, Q2, etc...

It's likely the process he's posting to, but I was just curious if anyone knew of a way to control the order of the fields as they're posted in.

Anyway, I think I'll just tell him that's pretty much the way it is.

whammy
10-13-2003, 05:40 PM
Well if he can get whoever he posts it to to use request.form.count... this is definitely something that has to be ordered server-side though.

BigDaddy
10-13-2003, 06:23 PM
Exactly...

Of course, it's a CGI process...so whatever he uses.


Anyway...thanks for all who gave input...probably should have gone to the general web board, as it wasn't really an ASP question.

:D

M@rco
10-14-2003, 12:43 AM
Actually, the order of the fields is determined by the method by which they are retrieved. Please see here:
http://www.aspfaqs.com/aspfaqs/ShowFAQ.asp?FAQID=64

;)

whammy
10-14-2003, 03:02 AM
That's the same solution I just posted, actually, but that reference didn't include:

Request.Form.Key(i) 'Where i is a looping integer.

All you need is:


'Debug
Dim i, Variables
For i = 1 To Request.Form.Count
Variables = Variables & i & ". " & Request.Form.Key(i) & ": " & Request.Form(i) & _
" (" & Len(Request.Form(i)) & ")" & "<br />" & vbCrLf
Next
Response.Write(Variables)


;)

M@rco
10-14-2003, 12:01 PM
Whammy, given N number of (uniquely named) fields posted to the page, your first posted solution had an outer loop of 1 to N, and then an inner loop from 1 to Q where 1<=Q<=N. Therefore it could potentially loop a maximum of N^2 times! This inner loop is totally unnecessary!

Not only are multiple values strung together with a comma (and a space) automatically if you access them from the main Request.Form collection, but if you need them in some other format (delimited with a pipe character, or perhaps stored in an array) then you can access them individually by using Request.Form(FieldNumber).Count and Request.Form(FieldNumber).Item(ValueNumber).

So all that is a required is an outer loop from 1 to N, a test to see if Request.Form(FieldNum).Count > 1, and if it is then an inner loop from 1 to Request.Form(FieldNum).Count to pull out the individual values.

i.e. something like this (with a few minor tweaks added for good measure):
<%
'grab the Request.Form.Count only once for efficiency
N = Request.Form.Count
For FieldNum = 1 To N
'reset the value counter
ValueCounter = 0

'grab the value count only once for efficiency
ValueCount = Request.Form(FieldNum).Count

'do we have multiple values?
If (ValueCount > 1) Then
'handle multiple field values
For ValueNum = 1 To ValueCount
Value = Trim(Request.Form(FieldNum)(ValueNum))

'see if we have something worth storing
If (Not IsBlank(Value)) Then
ValueCounter = ValueCounter + 1

'build the pipe-delimited value string
If (ValueCounter > 1) Then
ValueString = ValueString & "|" & Value
Else
ValueString = Value
End if
End if
Next
Else
'single field value
ValueString = Request.Form(FieldNum)
End if

'now do something with the value string we have created
'e.g. FieldDictionary.Add Request.Form.Key(FieldNum), ValueString
Next
%>

:D

Please note:
Although *all* my code is 100% homegrown, having spent years answering the same questions, I have a reduced inclination to write code myself for every question I answer, particularly when it is rather basic or more than adequately covered elsewhere. I therefore provide links, pseudo-code, or a text description instead. I was hoping to get away with the links I had posted earlier (the 4Guys link did explain it well), but I relented and wrote the above code (just for once)! ;)

BigDaddy
10-14-2003, 03:14 PM
** Jaw dropping ** Ahhhhhh.......WE'RE NOT WORTHY!!! :D

Seriously, though--I learned something new today. I like the idea of referencing a request.form item by count. That's pretty slick.

whammy
10-15-2003, 04:34 AM
Where are there two loops? I'm missing something... or you are m@rco... and isn't your code a lot more bloated, longer to process, and unnecessary? :confused:

I think perhaps you're referencing checkboxes, or something of that nature, which I handle in an altogether different manner (in conjunction with this technique).

I'm using _one_ loop through the Request.Form collection, creating a string out of it, and printing it.

I think perhaps you are missing (or adding) something instead.

There is no inner or outer loop... it's just one loop through the Request.Form collection. Care to explain how you came to the conclusion that I was using multiple loops in this scenario?

If you are curious to find out some efficient ways to handle checkboxes, etc. I can probably help you out. :D It seems like you may need the help, regarding the bloated code above. :p

If you're just talking about fields with the same name, I have a much easier way to handle those as well.

raf
10-15-2003, 08:45 AM
Where are there two loops?
I suppose he still refers to your first post.
for i = 1 to Request.Form.Count
for each name in Request.Form

Anyway, i still don't understand why everyone makes it that hard, when a simple


Dim i, item
i = 1
do while i <= 10
item = "q" & i
Response.Write(item & " = " & Request.form(item) & "<br />" & vbCrLf)
i = i + 1
loop

does the trick much more efficient. And if it needs to be generic, then just include 2 hidden formfields. (like name="namestart" value="quest" and name="num" value="10" ) using the form.count is quite useless cause this doesn't return the number of question-related formfields.
Like


Dim i, item
i = 1
do while i <= request.form("num")
item = request.form("namestart") & i
Response.Write(item & " = " & Request.form(item) & "<br />" & vbCrLf)
i = i + 1
loop

M@rco
10-15-2003, 11:02 AM
Originally posted by whammy
Where are there two loops? I'm missing something...


Originally posted by whammy
I think perhaps you're referencing checkboxes, or something of that nature, which I handle in an altogether different manner (in conjunction with this technique).

I'm using _one_ loop through the Request.Form collection, creating a string out of it, and printing it.

I think perhaps you are missing (or adding) something instead.

There is no inner or outer loop... it's just one loop through the Request.Form collection. Care to explain how you came to the conclusion that I was using multiple loops in this scenario?

Yes you are missing something (as raf has already pointed out), I was referring to the BrainJar code you posted:

Originally posted by M@rco
Whammy, given N number of (uniquely named) fields posted to the page, your first posted solution had an outer loop of 1 to N, and then an inner loop from 1 to Q where 1<=Q<=N.----------------------------
Originally posted by whammy
and isn't your code a lot more bloated, longer to process, and unnecessary? :confused:

No. Which bits are unnecessary? There is no more efficient way to parse the request form collection to obtain each field while handling multiple values. I defy you to prove otherwise! :p

Obviously I've added comments and code to build a delimited string because that's the output that it produces, but those can be removed and it all depends what you're actually doing with the retrieved values - whether you're storing them somewhere (e.g. a recordset or dictionary) or using them directly to do something. Everyone's code in this thread outputs the data slightly differently, and I would have thought that it is unlikely that BigDaddy will actually want to dump all the fields to screen in the way that his example does - it's far more likely that he wants to process the submitted fields in the order that they appear in the form, and thus any such code should be inserted where I had the commented-out FieldDictionary.Add line.
----------------------------

Originally posted by whammy
If you are curious to find out some efficient ways to handle checkboxes, etc. I can probably help you out. :D It seems like you may need the help, regarding the bloated code above. :pI do hope you're winding me up...! I'd be disappointed if you were seriously being *that* patronising! ;) Although not the object of the exercise, the script I posted will handle checkboxes and radio buttons without further modification, assuming you're using them correctly (i.e. according to W3C spec) in your form.
----------------------------

Originally posted by whammy
If you're just talking about fields with the same name, I have a much easier way to handle those as well.Go on then, show me what I'm missing! :p


Ok, your turn! ;)

whammy
10-15-2003, 04:05 PM
Actually I was "winding you up". ;)

It wasn't clear you were referring to brainjar's code, not mine, anyway. I'm not going to defend his code, I was just using it as an example. :D

Now any confusion should be cleared up... actually I like your code, especially the fact that you don't keep requesting the count, etc. which makes it more efficient. Looks similar to some things I've done in .js. :D

As far as easy ways to request checkboxes, disregard that, I'm sure you know just as many ways as I do. :P

M@rco
10-15-2003, 06:41 PM
Originally posted by whammy
It wasn't clear you were referring to brainjar's code, not mine, anyway.Yes it was! :p


Originally posted by whammy
Now any confusion should be cleared up... actually I like your code, especially the fact that you don't keep requesting the count, etc. which makes it more efficient. Looks similar to some things I've done in .js. :DThanks, glad it meets with your approval! :p


Originally posted by whammy As far as easy ways to request checkboxes, disregard that, I'm sure you know just as many ways as I do. :P [/B]Indeed :p


Are we done now? :D

whammy
10-17-2003, 03:54 AM
No. I'm sure after this that you're easily wound up. And actually you strike me as rather cocky; even more so than me (but aren't all brits? lol!) ;)

Why should I be done when it's so much fun? :D

P.S. Take a chill pill man. :)

I'm quite sure that we will be able to combine resources to either help one another out (!) or other people even easier than creating silly banter.

M@rco
10-17-2003, 09:28 AM
Originally posted by whammy
I'm quite sure that we will be able to combine resources to either help one another out (!) or other people even easier than creating silly banter. You started it! :p

But seriously folks, enough banter, as you say. :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum