Iterating through a collection of Request.Form fields

P

Paxton

At the risk of being told "If it ain't broke, don't fix it", my code works,
but is ugly.

Part of the admin site I'm working on at the moment includes a facility for
users to enter Formulations (recipes for making cosmetics etc) in 3 stages:

Stage 1: basic info such as title, method etc and number of Phases (steps in
recipe).
Stage 2: dynamically generated form containing the exact number of phases as
textboxes, depending on the number entered in stage 1. Phases are labelled
A, B, C...etc. They rarely exceed 8 in total. In each text box, the user
enters the number of ingredients per phase.
Stage 3: dynamically generated form offering the correct number of text
boxes per phase for users to enter ingredients per phase.

These inputs are named IngredientsA, IngredientsB etc - depending on the
phase they belong to. Of course, if there is more than one ingredients per
phase, they are posted back as a comma-delimited list.

At the moment, I test for the existence of each phase's ingredients:
if len(Request.Form("IngredientsA"))>0 then
....process the data
end if
if len(Request.Form("IngredientsB"))>0 then
....process the data
end if
if len(Request.Form("IngredientsC"))>0 then
....process the data
up to IngredientsJ.

Everything works as I would like it to, but I'm sure there must be a better
way of iterating through these form fields than this. Coding to cater for
up to 10 possible phases is repetitive, and is over 200 lines long.
Elegant, it ain't! Anyone got any ideas?

P
 
C

Chris Hohmann

Paxton said:
At the risk of being told "If it ain't broke, don't fix it", my code works,
but is ugly.

Part of the admin site I'm working on at the moment includes a facility for
users to enter Formulations (recipes for making cosmetics etc) in 3 stages:

Stage 1: basic info such as title, method etc and number of Phases (steps in
recipe).
Stage 2: dynamically generated form containing the exact number of phases as
textboxes, depending on the number entered in stage 1. Phases are labelled
A, B, C...etc. They rarely exceed 8 in total. In each text box, the user
enters the number of ingredients per phase.
Stage 3: dynamically generated form offering the correct number of text
boxes per phase for users to enter ingredients per phase.

These inputs are named IngredientsA, IngredientsB etc - depending on the
phase they belong to. Of course, if there is more than one ingredients per
phase, they are posted back as a comma-delimited list.

At the moment, I test for the existence of each phase's ingredients:
if len(Request.Form("IngredientsA"))>0 then
....process the data
end if
if len(Request.Form("IngredientsB"))>0 then
....process the data
end if
if len(Request.Form("IngredientsC"))>0 then
....process the data
up to IngredientsJ.

Everything works as I would like it to, but I'm sure there must be a better
way of iterating through these form fields than this. Coding to cater for
up to 10 possible phases is repetitive, and is over 200 lines long.
Elegant, it ain't! Anyone got any ideas?

http://aspfaq.com/show.asp?id=2036
 
P

Paxtonend

Chris Hohmann said:


Thanks for the response. It's obvious that I phrased my OP poorly.
This is an example of the code I'm currently processing:

productnames=Split(Request.Form("productnameA"),",")
inciname=Split(Request.Form("incinameA"),",")
suppliername=Split(Request.Form("supplierA"),",")
volume=Split(Request.Form("volumeA"),",")
for i = 0 to Ubound(productnames)
sqlstuff= "INSERT INTO FormulationIngredients (FormulationID, Phase,
ProductName, INCIName, Supplier, Volume) VALUES ('" &
Request.Form("FormulationID") & "','A','" & Trim(productnames(i))&
"','" & Trim(inciname(i)) & "','" & Trim(suppliername(i)) & "','" &
Trim(volume(i)) & "');"
Response.write productnames(i) & ", " & inciname(i)& ", " &
suppliername(i)&", " & volume(i)& "<br>"
oConn.execute(sqlstuff)
next

Then I have to test for the presence of Request.Form("productnameB"),
the productnameC, then D, etc etc, copying and pasting the code each
time, but changing the last character in the form field names.

As I say, I've created code to cope with all eventualities up to J,
and there's no reason why I can't continue to Z. But is there a
better way of doing this?

TIA
P
 
B

Bob Barrows [MVP]

Paxtonend said:
Thanks for the response. It's obvious that I phrased my OP poorly.
This is an example of the code I'm currently processing:

productnames=Split(Request.Form("productnameA"),",")
inciname=Split(Request.Form("incinameA"),",")
suppliername=Split(Request.Form("supplierA"),",")
volume=Split(Request.Form("volumeA"),",")
for i = 0 to Ubound(productnames)
sqlstuff= "INSERT INTO FormulationIngredients (FormulationID, Phase,
ProductName, INCIName, Supplier, Volume) VALUES ('" &
Request.Form("FormulationID") & "','A','" & Trim(productnames(i))&
"','" & Trim(inciname(i)) & "','" & Trim(suppliername(i)) & "','" &
Trim(volume(i)) & "');"
Response.write productnames(i) & ", " & inciname(i)& ", " &
suppliername(i)&", " & volume(i)& "<br>"
oConn.execute(sqlstuff)
next

Then I have to test for the presence of Request.Form("productnameB"),
the productnameC, then D, etc etc, copying and pasting the code each
time, but changing the last character in the form field names.

As I say, I've created code to cope with all eventualities up to J,
and there's no reason why I can't continue to Z. But is there a
better way of doing this?

TIA
P
Why not a nested loop?

for i = 65 to 90
productnames=Split(Request.Form("productname" & chr(i)),",")
inciname=Split(Request.Form("inciname" & chr(i)),",")
...
for j = 0 to Ubound(productnames)
...
next
next

Bob Barrows
 
P

Paxton

Bob said:
Why not a nested loop?

for i = 65 to 90
productnames=Split(Request.Form("productname" & chr(i)),",")
inciname=Split(Request.Form("inciname" & chr(i)),",")
...
for j = 0 to Ubound(productnames)
...
next
next

Bob Barrows


Since I used that method to create the names of the form fields
(appending the letter to create the field names) in the first place, it
makes perfect sense to use it to unpick them again. Many thanks.

P
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top