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 Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

A Variable, Variable Name

Status
Not open for further replies.

davecapone

Programmer
Oct 3, 2000
48
US
I am looking to populate variable name using a variable. It is kind of hard to explain so let me write it out in asp code a bit.

<%
for each varName in request.form
varName=request.form(varName&quot;)
next
%>

In other words if I have 20 variables on a form with a given name, I want to create standalone variables with those names containing the data of the form fields. The reason I'm looking to do this is I have a script that based upon a certain field will either use data enter in a form or pull data from a database and I think the code would be more efficient is the logic of where it is to come from is made once and populated in simple variable names instead of testing where the data is located and using rd(varName) or request.form(varName) in every place I need to use/manipulate the data.

Is there a way to do what I asked or another way to accomplish what I would like to accomplish?

Thanks,
David Capone
 
Your logic of using variable names that are the same names as the form variables is good, and helps keep things in line.

It is good practice to dim all your variables, but you cannot dynamically dim the variables, which is what you would need to do.

I recommend either dimming them one by one ie writing them out, or using an array.

If you want to explore the path you have outlined, the index number for the form collection will help you. Jonathan Galpin
 
Could you explain what the form collection index number is/does and how to retrieve. The key is I have a form with 25 fields and am a huge fan of neat code so I was hoping there was a way I could dynamically initialize variables and/or an array so I didn't have a full page list of variable initializations.

Thanks again,
David Capone
 
Form Collection:

Try this, test to verify:
*********************************
for i = 1 to Request.Form.Count
response.write Request.Form(i).Item & &quot;<br>&quot;
next
*********************************

or this:
*********************************
for each item in Request.Form
response.write Request.Form.Item & &quot;<br>&quot;
next
*********************************

or this:
*********************************
for each key in request.form
response.write request.form(request.form.key).item & &quot;<br>&quot;
next
*********************************

The &quot;request.form.key&quot; property is the index number. I do not have time to test, so figure it out by running a few tests, writing the info to the screen.
Jonathan Galpin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top