Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1914
  • Last Modified:

for each item in request.form

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
asabi
Asked:
asabi
  • 12
  • 8
  • 6
  • +3
1 Solution
 
peteyhigginsCommented:
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
 
peteyhigginsCommented:
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
 
John844Commented:
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
stefanxCommented:
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
 
John844Commented:
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
 
John844Commented:
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
 
asabiAuthor Commented:
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
 
John844Commented:
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
 
John844Commented:
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
 
asabiAuthor Commented:
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
 
John844Commented:
<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
 
turbosigCommented:
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
 
turbosigCommented:
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
 
turbosigCommented:
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
 
asabiAuthor Commented:
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
 
stefanxCommented:
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
 
AlfaNoMoreCommented:
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
 
AlfaNoMoreCommented:
ooops. that hidden field should be: <input type="hidden" name="form_order" value="XXX,YYY,ZZZ">

Wish I could type!!!
0
 
turbosigCommented:
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
 
turbosigCommented:
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
 
turbosigCommented:
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
 
John844Commented:
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
 
turbosigCommented:
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
 
John844Commented:
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
 
turbosigCommented:
Well, I certainly agree there.  I am POSITIVE one of these solutions will work.
0
 
asabiAuthor Commented:
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
 
AlfaNoMoreCommented:
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
 
asabiAuthor Commented:
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
 
turbosigCommented:
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
 
turbosigCommented:
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
 
turbosigCommented:
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
 
asabiAuthor Commented:
I guess that's the closest thing ..

I can see my confusion now.

Thanks for everybody.

0
 
turbosigCommented:
I am just glad we were able to get this straight.
0

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

  • 12
  • 8
  • 6
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now