Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

indexing form elements

Status
Not open for further replies.

blueindian1

Programmer
Apr 24, 2001
150
US
Hello All,

Let me tell you what I need to do. I have a form from which I need to caputre values to put into an email. Lets say the form looks like this:

Code:
<form name=&quot;formName&quot; method=&quot;Post&quot;>
<input type=&quot;text&quot; name=&quot;Input1&quot;>
<input type=&quot;text&quot; name=&quot;Input2&quot;>
<input type=&quot;text&quot; name=&quot;Input3&quot;>
<input type=&quot;text&quot; name=&quot;Input4&quot;>
</form>

Now after I post the form, I want to loop through the form elements and capture the values to put into the email. I will use the name for the form element as a label on the email with the value beside it, so the message might look something like this:

To: someone@somewhere.com
From: me@overhere.com
Subject: A Message

Input1: posted value of input 1
Input2: posted value of input 2
Input3: posted value of input 3
Input4: posted value of input 4


Now, what I want to do is generate this email by dynamically looping through the form elements. I don't want to have to hardcode a request for each element. So my question is, is there someway I can index the form elements to control the order they get looped on? In other words, I need to control the order in which the form elements are caputered. Basically, I need the subject of the email to look like this:

Input4: posted value of input 4
Input2: posted value of input 2
Input3: posted value of input 3
Input1: posted value of input 1


I hope this question makes sense, I've tried to make it as clear as I can.

Thanks in advance to anyone who can help

Rich


 
dim i, currentName, currentValue
for i = 0 to request.form.count
currentName = request.form.key(i)
currentValue = request.form(i)
next

should fix you right up... do whatever you need to do with those values (which obviously change on each iteration of the loop, so you better be quick ;-) ).

good luck! :)
paul
penny.gif
penny.gif
 
link9:

Thanks..

I've got that part, but what I need to do is specify the key in the input tag someway. Like, from my example, I need to get Input4 first, and then Input2, and then Input 1, and so on.

The code you posted will allow me to capture all the values for sure, but I don't see how it will let me control what order I capture them in.
 
Well, if all you need to do is capture them backwards, then do this:

dim i
i = request.form.count
while i >= 0
'do your thing here, as posted above
i = i - 1
wend

I don't think I get the purpose of your question... I understand what you're asking, but it seems to me that if you have some predefined order of grabbing the elements, and that pattern has no mathematical method that could simulate it, then you're going to have to hard code it somehow. Whether it be right in the code, or having the element ordinal positions stored in an array, and letting that specify what the order is, or whatever else...

Is there some method to the retrieval that could be simulated w/ a generic algorithm? I guess that's the real question. You can grab them in any order you like.

Does that help?
paul
penny.gif
penny.gif
 
Well, you are right. It will have to be hardcoded somewhere, because the order cannont be simulated by any sort of algorithm. But, the thing is, this page has about 120 form elements. So if I could specify some sort of index in the input tag, my looping logic to pull the values in the order I want would be much simpler.

So I would have:

Code:
dim i, currentName, currentValue
for i = 0 to request.form.count
  'where i would be the one I want next, as defined by 
  'some sort of index on the tag
next

versus some big select case or nested if for all 120 that would involve something like:
Code:
dim i, currentName, currentValue
for i = 0 to request.form.count
  if i = theOneIWantNext
  then
     'do something
next

but, it appears from here and other sources that that is what i'll have to do.

thanks for your help
 
Well, if it must be that way, then I would suggest the array solution, rather than some big convoluted select case statement or some such....

dim myArray()
redim myArray(request.form.count)
'pack it in order
' do this in an include file so as not to junk up your code
myArray(0) = 5
myArray(1) = 50

and so on. Then, jump in a loop:

dim i, theValue
for i = 0 to ubound(myArray)
theValue = request.form(myArray(i))
next

So that the resulting code isn't really much nastier than some other method.

:)
paul
penny.gif
penny.gif
 
not a bad idea...I may give that a whirl.

thanks for all your help
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top