Iterating through a collection of Request.Form fields

Discussion in 'ASP General' started by Paxton, Nov 19, 2004.

  1. Paxton

    Paxton Guest

    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
    Paxton, Nov 19, 2004
    #1
    1. Advertising

  2. "Paxton" <paxtonend@[no-spam]hotmail.com> wrote in message
    news:M8tnd.7143$...
    > 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
    Chris Hohmann, Nov 19, 2004
    #2
    1. Advertising

  3. Paxton

    Paxtonend Guest

    "Chris Hohmann" <> wrote in message news:<OQu8H#>...
    > "Paxton" <paxtonend@[no-spam]hotmail.com> wrote in message
    > news:M8tnd.7143$...
    > > 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
    Paxtonend, Nov 22, 2004
    #3
  4. Paxtonend wrote:
    > "Chris Hohmann" <> wrote in message
    > news:<OQu8H#>...
    >> "Paxton" <paxtonend@[no-spam]hotmail.com> wrote in message
    >> news:M8tnd.7143$...
    >>> 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.
    Bob Barrows [MVP], Nov 22, 2004
    #4
  5. Paxton

    Paxton Guest

    Bob Barrows [MVP] wrote:
    > Paxtonend wrote:
    >
    >>"Chris Hohmann" <> wrote in message
    >>news:<OQu8H#>...
    >>
    >>>"Paxton" <paxtonend@[no-spam]hotmail.com> wrote in message
    >>>news:M8tnd.7143$...
    >>>
    >>>>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
    Paxton, Nov 22, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Milsnips
    Replies:
    4
    Views:
    2,335
    Scott M.
    Oct 30, 2004
  2. Øyvind Isaksen
    Replies:
    1
    Views:
    941
    Øyvind Isaksen
    May 18, 2007
  3. carl
    Replies:
    5
    Views:
    2,330
    James Kanze
    Nov 25, 2009
  4. Peter Bailey
    Replies:
    6
    Views:
    121
    Peter Bailey
    Feb 16, 2007
  5. Mafj
    Replies:
    4
    Views:
    214
Loading...

Share This Page