473,566 Members | 2,785 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.For m("IngredientsA "))>0 then
....process the data
end if
if len(Request.For m("IngredientsB "))>0 then
....process the data
end if
if len(Request.For m("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 4278
"Paxton" <paxtonend@[no-spam]hotmail.com> wrote in message
news:M8******** ********@fe2.ne ws.blueyonder.c o.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.For m("IngredientsA "))>0 then
....process the data
end if
if len(Request.For m("IngredientsB "))>0 then
....process the data
end if
if len(Request.For m("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****@thankyo u.com> wrote in message news:<OQ******* *******@TK2MSFT NGP14.phx.gbl>. ..
"Paxton" <paxtonend@[no-spam]hotmail.com> wrote in message
news:M8******** ********@fe2.ne ws.blueyonder.c o.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.For m("IngredientsA "))>0 then
....process the data
end if
if len(Request.For m("IngredientsB "))>0 then
....process the data
end if
if len(Request.For m("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=Sp lit(Request.For m("productnameA "),",")
inciname=Split( Request.Form("i ncinameA"),",")
suppliername=Sp lit(Request.For m("supplierA"), ",")
volume=Split(Re quest.Form("vol umeA"),",")
for i = 0 to Ubound(productn ames)
sqlstuff= "INSERT INTO FormulationIngr edients (FormulationID, Phase,
ProductName, INCIName, Supplier, Volume) VALUES ('" &
Request.Form("F ormulationID") & "','A','" & Trim(productnam es(i))&
"','" & Trim(inciname(i )) & "','" & Trim(supplierna me(i)) & "','" &
Trim(volume(i)) & "');"
Response.write productnames(i) & ", " & inciname(i)& ", " &
suppliername(i) &", " & volume(i)& "<br>"
oConn.execute(s qlstuff)
next

Then I have to test for the presence of Request.Form("p roductnameB"),
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****@thankyo u.com> wrote in message
news:<OQ******* *******@TK2MSFT NGP14.phx.gbl>. ..
"Paxton" <paxtonend@[no-spam]hotmail.com> wrote in message
news:M8******** ********@fe2.ne ws.blueyonder.c o.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.For m("IngredientsA "))>0 then
....process the data
end if
if len(Request.For m("IngredientsB "))>0 then
....process the data
end if
if len(Request.For m("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=Sp lit(Request.For m("productnameA "),",")
inciname=Split( Request.Form("i ncinameA"),",")
suppliername=Sp lit(Request.For m("supplierA"), ",")
volume=Split(Re quest.Form("vol umeA"),",")
for i = 0 to Ubound(productn ames)
sqlstuff= "INSERT INTO FormulationIngr edients (FormulationID, Phase,
ProductName, INCIName, Supplier, Volume) VALUES ('" &
Request.Form("F ormulationID") & "','A','" & Trim(productnam es(i))&
"','" & Trim(inciname(i )) & "','" & Trim(supplierna me(i)) & "','" &
Trim(volume(i)) & "');"
Response.write productnames(i) & ", " & inciname(i)& ", " &
suppliername(i) &", " & volume(i)& "<br>"
oConn.execute(s qlstuff)
next

Then I have to test for the presence of Request.Form("p roductnameB"),
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=Sp lit(Request.For m("productnam e" & chr(i)),",")
inciname=Split( Request.Form("i nciname" & chr(i)),",")
...
for j = 0 to Ubound(productn ames)
...
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****@thankyo u.com> wrote in message
news:<OQ***** *********@TK2MS FTNGP14.phx.gbl >...
"Paxton" <paxtonend@[no-spam]hotmail.com> wrote in message
news:M8***** ***********@fe2 .news.blueyonde r.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
ingredien ts

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.For m("IngredientsA "))>0 then
....process the data
end if
if len(Request.For m("IngredientsB "))>0 then
....process the data
end if
if len(Request.For m("IngredientsC "))>0 then
....process the data
up to IngredientsJ.

Everythin g 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.F orm("productnam eA"),",")
inciname=Spli t(Request.Form( "incinameA"),", ")
suppliername= Split(Request.F orm("supplierA" ),",")
volume=Split( Request.Form("v olumeA"),",")
for i = 0 to Ubound(productn ames)
sqlstuff= "INSERT INTO FormulationIngr edients (FormulationID, Phase,
ProductName , INCIName, Supplier, Volume) VALUES ('" &
Request.Form( "FormulationID" ) & "','A','" & Trim(productnam es(i))&
"','" & Trim(inciname(i )) & "','" & Trim(supplierna me(i)) & "','" &
Trim(volume(i )) & "');"
Response.writ e productnames(i) & ", " & inciname(i)& ", " &
suppliername( i)&", " & volume(i)& "<br>"
oConn.execute (sqlstuff)
next

Then I have to test for the presence of Request.Form("p roductnameB"),
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=Sp lit(Request.For m("productnam e" & chr(i)),",")
inciname=Split( Request.Form("i nciname" & chr(i)),",")
...
for j = 0 to Ubound(productn ames)
...
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1809
by: Patrick von Harsdorf | last post by:
I want to iterate over a collection and delete all unwanted entries. for item in collection: del item of course doesn´t do anything useful. Two things come to mind: a) iterating backwards over the collection using indexing, something like: for i in range(len(collection)-1, -1, -1): item = collection
6
2282
by: Agoston Bejo | last post by:
Hi. x1.asp: <form method="post" action="x2.asp"> .... </form> x2.asp: DoSomeAdministration() Response.Redirect "x3.asp?" & Request.Form x3.asp: further processsing of data
5
3553
by: mvr | last post by:
Hi all IIS 5.0, ASP, and https:// I have "DataEntrypage.asp" which is a data entry page(about 250 data elements includes text boxes, radio buttons, check boxes, drop down boxes etc). After the data validation through javascript(form.action = "ProcessData.asp" )I post this page to "ProcessData.asp" which process all
0
1561
by: Kathy Burke | last post by:
Hi, I have an html form (created by transforming an xml doc with an xsl stylesheet and sent as html within an asp.net page. The doc is a set of user instructions steps. For each of the elements that allow user input (<measurement> and <data_collection>, for example, my xsl turns that into an <input type=text name="the unique name...
10
6901
by: Kathy Burke | last post by:
HI. in asp.net app, I have an xmlDocument that I transform to the client html. Using xsl I create a few textboxes to capture user input. Each of these are related to <data> elements in the xmlDoc. I want to use the Forms collection to post the html form back to an asp.net page, and process each request.form object (textbox) via an xml...
4
1678
by: John Buchmann | last post by:
I thought this would not be difficult, but i'm stumped! I need to iterate through a bunch of form fields, and read the data (value, text, etc.) from them. (I need to iterate through them programmatically because it is not known ahead of time which form fields will be displayed on the browser. The fields will be displayed or hidden...
17
15528
by: ronaldlee | last post by:
I have this error in Line 89. Collection is read-only. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.NotSupportedException: Collection is read-only.
0
1460
by: ronaldlee | last post by:
I got a collection read only error, below is the error message. Collection is read-only. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.NotSupportedException:...
2
5038
by: David Veeneman | last post by:
Is there a way to iterate the components on a form? I need to determine whether an instance of my custom component (a System.ComponentModel component) is present in a form. I have tried iterating the Controls collection, but components do not appear to be in that collection-- I think it's because components inherit from MarshalByRefObject,...
0
7666
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7888
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7644
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7951
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6260
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5484
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
1
2083
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1201
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
925
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.