Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    Regular Coder
    Join Date
    Sep 2002
    Location
    Bugaha, NE
    Posts
    330
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Order of fields posted?

    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.
    This is in spanish when you're not looking.

  • #2
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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
    Code:
    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

  • #3
    New Coder
    Join Date
    Jun 2002
    Location
    Pilipinas
    Posts
    89
    Thanks
    0
    Thanked 0 Times in 0 Posts
    hmmm, not sure, but it would most probably be in the same order as how the fields are sequentially accessed in the form itself.

    http://authors.aspalliance.com/aspxt...ClassForm.aspx

  • #4
    Senior Coder
    Join Date
    Jun 2002
    Location
    41° 8' 52" N -95° 53' 31" W
    Posts
    3,660
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    Former ASP Forum Moderator - I'm back!

    If you can teach yourself how to learn, you can learn anything. ;)

  • #5
    Regular Coder
    Join Date
    Sep 2002
    Location
    Bugaha, NE
    Posts
    330
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    This is in spanish when you're not looking.

  • #6
    Senior Coder
    Join Date
    Jun 2002
    Location
    41° 8' 52" N -95° 53' 31" W
    Posts
    3,660
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    Former ASP Forum Moderator - I'm back!

    If you can teach yourself how to learn, you can learn anything. ;)

  • #7
    Regular Coder
    Join Date
    Sep 2002
    Location
    Bugaha, NE
    Posts
    330
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

    This is in spanish when you're not looking.

  • #8
    Regular Coder
    Join Date
    Oct 2003
    Location
    London, UK
    Posts
    411
    Thanks
    0
    Thanked 1 Time in 1 Post
    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

    Marcus Tucker / www / blog
    Web Analyst Programmer / Voted SPF "ASP Guru"

  • #9
    Senior Coder
    Join Date
    Jun 2002
    Location
    41° 8' 52" N -95° 53' 31" W
    Posts
    3,660
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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:
    Code:
    '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)
    Last edited by whammy; 10-14-2003 at 03:05 AM.
    Former ASP Forum Moderator - I'm back!

    If you can teach yourself how to learn, you can learn anything. ;)

  • #10
    Regular Coder
    Join Date
    Oct 2003
    Location
    London, UK
    Posts
    411
    Thanks
    0
    Thanked 1 Time in 1 Post
    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):
    Code:
    <%
    		'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
    %>


    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)!
    Last edited by M@rco; 10-14-2003 at 12:06 PM.
    Marcus Tucker / www / blog
    Web Analyst Programmer / Voted SPF "ASP Guru"

  • #11
    Regular Coder
    Join Date
    Sep 2002
    Location
    Bugaha, NE
    Posts
    330
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ** Jaw dropping ** Ahhhhhh.......WE'RE NOT WORTHY!!!

    Seriously, though--I learned something new today. I like the idea of referencing a request.form item by count. That's pretty slick.
    This is in spanish when you're not looking.

  • #12
    Senior Coder
    Join Date
    Jun 2002
    Location
    41° 8' 52" N -95° 53' 31" W
    Posts
    3,660
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?

    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. It seems like you may need the help, regarding the bloated code above.

    If you're just talking about fields with the same name, I have a much easier way to handle those as well.
    Last edited by whammy; 10-15-2003 at 05:12 AM.
    Former ASP Forum Moderator - I'm back!

    If you can teach yourself how to learn, you can learn anything. ;)

  • #13
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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
    Code:
    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
    Code:
    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

  • #14
    Regular Coder
    Join Date
    Oct 2003
    Location
    London, UK
    Posts
    411
    Thanks
    0
    Thanked 1 Time in 1 Post
    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?
    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!

    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. It seems like you may need the help, regarding the bloated code above.
    I 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!


    Ok, your turn!
    Marcus Tucker / www / blog
    Web Analyst Programmer / Voted SPF "ASP Guru"

  • #15
    Senior Coder
    Join Date
    Jun 2002
    Location
    41° 8' 52" N -95° 53' 31" W
    Posts
    3,660
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

    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.

    As far as easy ways to request checkboxes, disregard that, I'm sure you know just as many ways as I do. :P
    Last edited by whammy; 10-15-2003 at 04:09 PM.
    Former ASP Forum Moderator - I'm back!

    If you can teach yourself how to learn, you can learn anything. ;)


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •