472,127 Members | 2,111 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,127 software developers and data experts.

Iterating through a collection of Request.Form fields

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
Jul 22 '05 #1
4 4193
"Paxton" <paxtonend@[no-spam]hotmail.com> wrote in message
news:M8****************@fe2.news.blueyonder.co.uk. ..
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
Jul 22 '05 #2
"Chris Hohmann" <no****@thankyou.com> wrote in message news:<OQ**************@TK2MSFTNGP14.phx.gbl>...
"Paxton" <paxtonend@[no-spam]hotmail.com> wrote in message
news:M8****************@fe2.news.blueyonder.co.uk. ..
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

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
Jul 22 '05 #3
Paxtonend wrote:
"Chris Hohmann" <no****@thankyou.com> wrote in message
news:<OQ**************@TK2MSFTNGP14.phx.gbl>...
"Paxton" <paxtonend@[no-spam]hotmail.com> wrote in message
news:M8****************@fe2.news.blueyonder.co.uk. ..
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

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
--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.
Jul 22 '05 #4
Bob Barrows [MVP] wrote:
Paxtonend wrote:
"Chris Hohmann" <no****@thankyou.com> wrote in message
news:<OQ**************@TK2MSFTNGP14.phx.gbl>.. .
"Paxton" <paxtonend@[no-spam]hotmail.com> wrote in message
news:M8****************@fe2.news.blueyonder.co. uk...

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

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

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
Jul 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Patrick von Harsdorf | last post: by
4 posts views Thread by John Buchmann | last post: by
17 posts views Thread by ronaldlee | last post: by
reply views Thread by ronaldlee | last post: by
2 posts views Thread by David Veeneman | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.