Solved

for each item in request.form

Posted on 2001-08-23
33
1,853 Views
Last Modified: 2012-06-21
for each item in request.form

returns the form elements in "random order".
there is a way to make it return the values in the order of the form, but I don't remember how ..
0
Comment
Question by:asabi
  • 12
  • 8
  • 6
  • +3
33 Comments
 
LVL 3

Expert Comment

by:peteyhiggins
ID: 6419356
Just loop through the Forms.Count

dim frm as Form

For i = 0 to Forms.Count - 1

set frm = Forms(i)

....'Do what you want with it here

Next
0
 
LVL 3

Expert Comment

by:peteyhiggins
ID: 6419371
Oops, I gave you it for looping through forms in vb.

For the requets.form in asp, its:

for i = 0 to request.form.count - 1

... 'Do stuff here

next
0
 
LVL 7

Expert Comment

by:John844
ID: 6419378
here you go

<%
dim frm
for each frm in Request.Form
  Response.Write "found form item " & frm & " = " & request(frm) & "<BR>"
next
%>

<form method=post>
<input type=text name=txt1 value="test">
<input type=text name=txt2 value="test2">
<INPUT type="submit" value="Submit" id=submit1 name=submit1>
</form>
0
 
LVL 8

Expert Comment

by:stefanx
ID: 6419491
Guys, I think you are misunderstanding the question. Every one knows you can say:

for each X in request.From
  Response.Write X & "=" & Request.Form(X) & "<BR>"
next

The issue is that when you loop through these elements (i.e X), they are not returned in the order as they are originally specified in the form. For example, if the form is defined as :

<form method=post action="myprocess.asp">
<input name="XXX">
<input name="YYY">
<input name=ZZZ">
</form>

then you do not get X in the order of XXX,YYY,ZZZ. Asabi is asking if there is any way of knowing the in which one can determine the order of the form elements.

I actually need top know this myself, because that's what one needs to write something like a generic form to e-mail poster.

Or asabi, did I get your question wrong as well ? ;)
0
 
LVL 7

Expert Comment

by:John844
ID: 6419532
ok, now I do see it, here you go:


dim aryFormItems
dim arySingleItem
dim strFormItemName
dim strFormItemValue
dim i
aryFormItems = split(Request.Form,"&")
for i = 0 to ubound(aryFormItems)
     arySingleItem = split(aryFormItems(i),"=")
     if ubound(arySingleItem) >= 0 then
          strFormItemName = arySingleItem(0)
     end if
     if ubound(arySingleItem) > 0 then
          strFormItemValue = arySingleItem(1)
     end if
  Response.Write "found form item " & strFormItemName & " = " & strFormItemValue & "<BR>"
next

%>
<P>&nbsp;</P>
<form method=post>
<input type=text name=txt1 value="test">
<input type=text name=txt2 value="test2">
<input type=text name=txt3 value="test3">
<input type=text name=txt4 value="test4">
<input type=text name=txt5 value="test5">
<input type=text name=txt6 value="test6">
<input type=text name=txt7 value="test7">
<input type=text name=txt8 value="test8">
<input type=text name=txt9 value="test9">
<input type=text name=txt10 value="test10">
<input type=text name=txt11 value="test11">
<input type=text name=txt12 value="test12">
<INPUT type="submit" value="Submit" id=submit1 name=submit1>
</form>
0
 
LVL 7

Expert Comment

by:John844
ID: 6419567
this will show the two variations for comparison and to verify the modifications helped.  Also added some comments.


<%@ Language=VBScript %>
<HTML>
<BODY>
form contents using for each(may be out of order)<br>
<%
dim frm
for each frm in Request.Form
  Response.Write "found form item " & frm & " = " & request(frm) & "<BR>"
next
%>


<BR>
<BR>
<BR>
form contents using request.form(should be in order)<br>

<%
dim aryFormItems
dim arySingleItem
dim strFormItemName
dim strFormItemValue
dim i

'grab all of the form items and split them into name,value pairs
aryFormItems = split(Request.Form,"&")

'loop through all of the pairs
for i = 0 to ubound(aryFormItems)
     'split this pair into name and value
     arySingleItem = split(aryFormItems(i),"=")

     'if we got something before the = sign, set a value for name
     if ubound(arySingleItem) >= 0 then
          strFormItemName = arySingleItem(0)
     end if

     'if we got something after the = sign, set a value for value
     if ubound(arySingleItem) > 0 then
          strFormItemValue = arySingleItem(1)
     end if
     
     'show the values
     Response.Write "found form item " & strFormItemName & " = " & strFormItemValue & "<BR>"
next

%>

<form method=post>
<input type=text name=txt1 value="test">
<input type=text name=txt2 value="test2">
<input type=text name=txt3 value="test3">
<input type=text name=txt4 value="test4">
<input type=text name=txt5 value="test5">
<input type=text name=txt6 value="test6">
<input type=text name=txt7 value="test7">
<input type=text name=txt8 value="test8">
<input type=text name=txt9 value="test9">
<input type=text name=txt10 value="test10">
<input type=text name=txt11 value="test11">
<input type=text name=txt12 value="test12">
<INPUT type="submit" value="Submit" id=submit1 name=submit1>
</form>
</BODY>
</HTML>
0
 
LVL 1

Author Comment

by:asabi
ID: 6419714
John844,

I understand the code, I did it before I posted my question, the problem is:

what happens if inside the form the user put something like

"me & you"

your whole information will be off because the split will take the & into account ...

am i right ?

Other ideas ?
0
 
LVL 7

Expert Comment

by:John844
ID: 6419737
it is not cut off, but you loose the benefit of having asp convert all of the special characters for you.  Page would return me+%26+you.  Additional logic would be needed to make this a full application.

John
0
 
LVL 7

Expert Comment

by:John844
ID: 6419749
here is a work around....

<%@ Language=VBScript %>
<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
</HEAD>
<BODY>
form contents using for each(may be out of order)<br>
<%
dim frm
for each frm in Request.Form
  Response.Write "found form item " & frm & " = " & request(frm) & "<BR>"
next
%>


<BR>
<BR>
<BR>
form contents using request.form(should be in order)<br>

<%
dim aryFormItems
dim arySingleItem
dim strFormItemName
'dim strFormItemValue
dim i

'grab all of the form items and split them into name,value pairs
aryFormItems = split(Request.Form,"&")

'loop through all of the pairs
for i = 0 to ubound(aryFormItems)
     'split this pair into name and value
     arySingleItem = split(aryFormItems(i),"=")

     'if we got something before the = sign, set a value for name
     if ubound(arySingleItem) >= 0 then
          strFormItemName = arySingleItem(0)
     end if

     'if we got something after the = sign, set a value for value
'     if ubound(arySingleItem) > 0 then
'          strFormItemValue = arySingleItem(1)
'     end if
     
     'show the values
     Response.Write "found form item " & strFormItemName & " = " & request(strFormItemName) & "<BR>"
next

%>

<form method=post>
<input type=text name=txt1 value="test">
<input type=text name=txt2 value="test2">
<input type=text name=txt3 value="test3">
<input type=text name=txt4 value="test4">
<input type=text name=txt5 value="test5">
<input type=text name=txt6 value="test6">
<input type=text name=txt7 value="test7">
<input type=text name=txt8 value="test8">
<input type=text name=txt9 value="test9">
<input type=text name=txt10 value="test10">
<input type=text name=txt11 value="test11">
<input type=text name=txt12 value="test12">
<INPUT type="submit" value="Submit" id=submit1 name=submit1>
</form>
</BODY>
</HTML>
0
 
LVL 1

Author Comment

by:asabi
ID: 6419779
John844,

I understand the code, I did it before I posted my question, the problem is:

what happens if inside the form the user put something like

"me & you"

your whole information will be off because the split will take the & into account ...

am i right ?

Other ideas ?
0
 
LVL 7

Accepted Solution

by:
John844 earned 50 total points
ID: 6419804
<am i right> no, try the code.  Or better yet, here is some a little cleaner with the changes from above.  Enter your test into one of the fields and see what is returned.  You will be surprised.

John





<%@ Language=VBScript %>
<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
</HEAD>
<BODY>
form contents using for each(may be out of order)<br>
<%
dim frm
for each frm in Request.Form
  Response.Write "found form item " & frm & " = " & request(frm) & "<BR>"
next
%>


<BR>
<BR>
<BR>
form contents using request.form(should be in order)<br>

<%
dim aryFormItems
dim arySingleItem
dim strFormItemName
dim i

'grab all of the form items and split them into name,value pairs
aryFormItems = split(Request.Form,"&")

'loop through all of the pairs
for i = 0 to ubound(aryFormItems)
     'split this pair into name and value
     arySingleItem = split(aryFormItems(i),"=")

     'Grab then first item from array, it is the form field name
     strFormItemName = arySingleItem(0)

     'get the value from the request collection using name found above
     'can't use second item in array unless we want to handle then encoding
     'value in arySingleItem(1) holds info like "me+%26+you"
     strFormItemValue = request(strFormItemName)
     
     'show the values
     Response.Write "found form item " & strFormItemName & " = " & strFormItemValue & "<BR>"
next


'build form
dim strTemp
Response.Write "<form method=post>" & vbCrLf
for i = 1 to 12
     strTemp = request("txt" & i)
     if strTemp = "" then
          strTemp = "test" & i
     end if
     Response.Write "<input type=text name=txt" & i & " value=""" & strTemp & """>" & vbCrLf
next
Response.Write "<INPUT type=submit value=Submit name=submit1>" & vbCrLf
Response.Write "</form>" & vbCrLf
%>

</BODY>
</HTML>
0
 
LVL 2

Expert Comment

by:turbosig
ID: 6420749
You could always do what we do.  We assign a user defined attribute to each input i.e.

<input type=text name=XX indexVal=1 value="test">
<input type=text name=ZZ indexVal=1 value="test2">
<input type=text name=YY indexVal=1 value="test3">

This way, you can name you fields what ever you want, any order, etc.

Although John844 has done all the work here just offering an alternative.
0
 
LVL 2

Expert Comment

by:turbosig
ID: 6420751
You could always do what we do.  We assign a user defined attribute to each input i.e.

<input type=text name=XX indexVal=1 value="test">
<input type=text name=ZZ indexVal=1 value="test2">
<input type=text name=YY indexVal=1 value="test3">

This way, you can name you fields what ever you want, any order, etc.

Although John844 has done all the work here just offering an alternative.
0
 
LVL 2

Expert Comment

by:turbosig
ID: 6420752
OOps, assign different indexval's 1, 2, 3, etc. Same concept as John844, but this way you could actually have them in any order, and then retrieve them inthe order you want>>


Anyway good luck
0
 
LVL 1

Author Comment

by:asabi
ID: 6420844
o.k, so now I have:

<input type=text name=XX indexVal=1 value="test">
<input type=text name=ZZ indexVal=2 value="test2">
<input type=text name=YY indexVal=3 value="test3">

How do i retrieve them ?

What is the ASP code to get them in that order ?
0
 
LVL 8

Expert Comment

by:stefanx
ID: 6420921
Yep, this sounds promising but it sounds to me as if you can only get that IndexVal under client side Javascript. It is needed server side ?
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 9

Expert Comment

by:AlfaNoMore
ID: 6421200
Maybe this is a workaround?

Post.htm

<input type="text" name="XXX">
<input type="text" name="YYY">
<input type="text" name="ZZZ">
<input type="hidden" name="form_order" value="XXX,YYY,XXX">

Process.asp

Dim astrOrder
Dim iLoop
Dim strThisValue

astrOrder = Split(Request.Form("form_order"),",")
For iLoop = lBound(astrOrder) To uBound(astrOrder)
   strThisValue = Request.Form(astrOrder(iLoop))
   '// now you have the form field in the order you requested it. But, you'd have to manually specify the order in the posting page! Still, it's your request.
Next


HTH?

Daniel Newman.
0
 
LVL 9

Expert Comment

by:AlfaNoMore
ID: 6421206
ooops. that hidden field should be: <input type="hidden" name="form_order" value="XXX,YYY,ZZZ">

Wish I could type!!!
0
 
LVL 2

Expert Comment

by:turbosig
ID: 6421783
Try this code.  you can play around with the speed and efficiency, but it does do what you want.

Hope this helps...

<html>
<head>
<script LANGUAGE="VBScript">
Sub button1_onClick
     Dim MyArray(10)

     set sObj = document.all("form1").all.tags("INPUT")
     for each sItem in sObj
          if sItem.indexVal <> "" then
               A = sItem.indexVal
               MyArray(A) = sItem.value
          end if
     next    
     
     For X = 0 to Ubound(MyArray)
          If Trim(MyArray(X)) <> "" then
               strFields = strFields  & MyArray(X) & ","
          end if
     Next
     
     strFields = Left(strFields,Len(strFields)-1)
     
     document.all("form1").txtFields.value = strFields
     form1.submit
End Sub    
</script>


</head>
<body>
<%
strFields = Request.Form("txtFields")
If strFields <> "" then

     allInputs=split(strFields,",")
     maxcounter=ubound(allInputs)

     For counter=0 TO maxcounter
       ThisInput=allInputs(counter)
       Response.Write "Index " & counter & " is the value: " & ThisInput & "<br>"
     Next
end if

strFields = ""
%>

<form method=post id=form1 name=form1 action="test4.asp">
Index 0: <input type=text id=text1 name=text1 indexVal=0 value="A"><BR>
Index 3: <input type=text id=text2 name=text2 indexVal=3 value="D"><BR>
Index 1: <input type=text id=text3 name=text3 indexVal=1 value="B"><BR>
Index 2: <input type=text id=text4 name=text4 indexVal=2 value="C"><BR>
Index 4: <input type=text id=text5 name=text5 indexVal=4 value="E"><BR>

<input type=hidden id=txtFields name=txtFields indexVal="">
</form>

<input type=button id=button1 name=button1 value="Get values">

</body>
</html>
0
 
LVL 2

Expert Comment

by:turbosig
ID: 6421789
Oh about the last code and the client side issue.

You grab the values on submission, add them to an input field that is hidden, then submit the form.

For even less over head you could also add the id of the form text fields to the array and just submit the single hidden field, then you do not have the over head of submitting ALL the form data.
0
 
LVL 2

Expert Comment

by:turbosig
ID: 6421797
Sorry for all the posts, but here is another idea.

use onload in the body and build an array of all the indexes.  Then when you do your clientside, you already have the index built??

Anyway just more ideas.
0
 
LVL 7

Expert Comment

by:John844
ID: 6421921
asabi, did my solution not give you the desired results?  If not, what was different from your needs.  Are you just looking for another way to do it?

John
0
 
LVL 2

Expert Comment

by:turbosig
ID: 6421992
John844,

Didn't mean to step on any toes.  I just did it this way so that you could name you fields what ever you wanted and not have to have a number in the name (id), basically putting that number in a seperate attribute which can be referenced?
0
 
LVL 7

Expert Comment

by:John844
ID: 6422044
no toes stepped on,  Just trying to find out exactly asabi did not like.  If it was something I could change, then I would.  If it was something I did not know how to correct, them maybe someone else here could help as well.  I am just trying to get some feedback from asabi so we can solve this problem and close the question in a reasonable amount of time.
John
0
 
LVL 2

Expert Comment

by:turbosig
ID: 6422245
Well, I certainly agree there.  I am POSITIVE one of these solutions will work.
0
 
LVL 1

Author Comment

by:asabi
ID: 6422511
John844,
When I post this message I already new your code
True - I haven't tried to test my notion about the "&" problem.
it is a good solution, and if no one else comes up with a better one, I will give you the points, but I remember that
I saw a simpler solution I know that you can't use something like:

For
request.form (counter)
Next

But I remember seeing a solution along this line.. so yes I am waiting just a bit more to see if anyone comes with a better solution.

About the "form_order", it will probably work but it is not really different from what JOHN suggested - you take all the fields and split them ..


0
 
LVL 9

Expert Comment

by:AlfaNoMore
ID: 6422539
except for the fact that they are in the order you have requested.

your querystring may be: zzz=7&yyy=9&xxx=98

you want them to be displayed in this order (xxx, yyy, zzz).

my solution does this for you, bacuase instead of just splitting the querystring and processing it in-line, you provide the sort order, and it uses that, thus providing your required order.

Yes, they are close, but mine is what you want!!!
0
 
LVL 1

Author Comment

by:asabi
ID: 6422636
What I really want is a way to do it without adding anything to the code in the form, it is complicated as it is.

The "program" is a totally generic solution, where the form is totally dynamic, it runs on virtually infinite number of dynamic steps I really don?t want to add more to it.

I am just looking for a more elegant way (I remember that there is one, I just can't remember how).


0
 
LVL 2

Expert Comment

by:turbosig
ID: 6423004
asabi, now I see where you are confused.  I believe below is what you are speaking of.  I have done two things, used count for a multiple field and also used the forms collection to loop through in the "corect order"

<%@ Language=VBScript %>
<HTML>
<HEAD>
</HEAD>
<BODY>

<b>This is used with a "specific" parameter</b><br>
<%
For i = 1 To Request.Form("color").Count
  Response.Write Request.Form("color")(i) & "<BR>"
Next
%>

<P>
<b>This is used with the forms collection</b><br>
<% For Each x In Request.Form %>
Request.Form( <%= x %> ) = <%= Request.Form(x) %> <BR>
<% Next %>
 

<FORM ACTION = "test2.asp" METHOD = "post" name=form1 id=form1>
<P>Your first name: <INPUT NAME = "firstname" SIZE =25>
<P>Your Last name: <INPUT NAME = "lastname" SIZE =25>
<P>Your Middle name: <INPUT NAME = "middlename" SIZE =25>
<P>What is your favorite color: <SELECT NAME = "color" MULTIPLE>
<OPTION>red
<OPTION>blue
<OPTION>green
<OPTION>orange
</SELECT>
<p><INPUT TYPE = SUBMIT>
</FORM>
 


</BODY>
</HTML>
0
 
LVL 2

Expert Comment

by:turbosig
ID: 6423032
OOOps, screwed up.  Here is the right code.

<%@ Language=VBScript %>
<HTML>
<HEAD>
</HEAD>
<BODY>

<b>This is used with a "specific" parameter</b><br>
<%
For i = 1 To Request.Form("text").Count
  Response.Write Request.Form("text")(i) & "<BR>"
Next
%>

<P>
<b>This is used with the forms collection</b><br>
<% For Each x In Request.Form %>
Request.Form( <%= x %> ) = <%= Request.Form(x) %> <BR>
<% Next %>
 
 <form ACTION = "test2.asp" METHOD = "post" name=form1 id=form1t>
<input type=text id=txt1 name=text value="test1">
<input type=text id=txt2 name=text value="test2">
<input type=text id=txt3 name=text  value="test3">
<input type=text id=txt4 name=text  value="test4">
<input type=text id=txt5 name=text  value="test5">
<input type=text id=txt6 name=text  value="test6">
<input type=text id=txt7 name=text  value="test7">
<input type=text id=txt8 name=text  value="test8">
<input type=text id=txt9 name=text value="test9">
<input type=text id=txt10 name=text  value="test10">
<input type=text id=txt11 name=text  value="test11">
<input type=text id=txt12 name=text  value="test12">
<INPUT type="submit" value="Submit" id=submit1 name=submit1>
</form>
 


</BODY>
</HTML>
0
 
LVL 2

Expert Comment

by:turbosig
ID: 6423047
The only problem doing it this way, the second post is that name can not be unique?  The is the essence of the index (or count) parameter.
0
 
LVL 1

Author Comment

by:asabi
ID: 6423316
I guess that's the closest thing ..

I can see my confusion now.

Thanks for everybody.

0
 
LVL 2

Expert Comment

by:turbosig
ID: 6423392
I am just glad we were able to get this straight.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now