Loop to create an array from a dynamic form.

Discussion in 'ASP General' started by Nick, Aug 26, 2004.

  1. Nick

    Nick Guest

    Loop to create an array from a dynamic form.

    I'm having trouble with an application, and I'll try to explain it as
    clearly as possible:

    1. I have a form with two fields, say Apples and Oranges. The user
    selects from a drop down menu, between 1-10 of each and clicks submit.
    The resulting page will display a NEW form, with rows and a list of
    fields for the amount of each items selected.

    Example. If I selected 3 apples and 5 oranges, the resulting page
    would show 3 rows of text fields for apples and 5 rows of text fields
    for oranges.

    The form fields would be (after each semicolon, the first word is the
    form element name (Apple1, Apple2)):

    Apple1: net price | gross price | date | time
    Apple2: net price | gross price | date | time
    Apple3: net price | gross price | date | time


    Orange1: net price | gross price | date | time
    Orange2: net price | gross price | date | time
    Orange3: net price | gross price | date | time
    Orange4: net price | gross price | date | time
    Orange5: net price | gross price | date | time


    I want to insert each one of these records into the database, each
    record being a net, gross price, date and time for apple 1-X and
    orange 1-X. The apples will go into the apple table and the oranges
    will go into the oranges table.


    I'm used to looping like this:

    for each item in request.form
    if left(item, 6) = "orange" then
    orange = orange & request.form(item) & ","
    end if
    next


    After that, I'll create a loop that inserts each record into the
    database separately, something of this nature:


    for each item in orange
    set rsOrange= server.createobject("adodb.recordset")
    strSql = "select * from oranges"
    rsOrange.open strSql, Connstr, 1, 3

    rsOrange.Addnew
    rsOrange("net") = request.form("net")
    rsOrange("gross") = request.form("gross")
    rsOrange("date") = request.form("date")
    rsOrange("time") = request.form("time")
    rsOrange.Update
    rsOrange.close
    next


    But since this is a multi dimensional array (gross, net, date, time
    will need to be inserted correspondingly), you can see my dilemma. I
    understand how they work when hard coded, but not to dynamically
    create and populate them, then use in an insert statement. Sorry if
    this is elementary, but any help would really be appreciated.

    Thanks!
    Nick, Aug 26, 2004
    #1
    1. Advertising

  2. You have to number your net, gross, date, and time form fields as well.
    (Why do you have separate date and time columns in your table? Why not just
    one field for this data?)

    Also, when you're doing INSERTs, don't create a recordset object.

    Try this code. (Three files)


    FORM.HTM:
    <html><head></head>
    <body>
    <form method="post" action="form.asp">
    Apples: <input name="apples" type="text" />
    Oranges: <input name="oranges" type="text" />
    <input type="submit" />
    </form>
    </body>
    </html>

    --------------------------------------
    FORM.ASP:
    <html><head></head>
    <body>
    <%
    Dim iOranges, iApples
    iApples = Request.Form("apples")
    iOranges = Request.Form("oranges")
    %>
    <form method="post" action="insert.asp">
    <input type="hidden" name="apples" value="<%=iApples%>" />
    <input type="hidden" name="oranges" value="<%=iOranges%>" />
    <table>
    <tr>
    <th colspan="5">Apples</th>
    </tr>
    <tr>
    <td>&nbsp;</td>
    <td>net price</td>
    <td>gross price</td>
    <td>date</td>
    <td>time</td>
    </tr>
    <% For i = 1 To iApples %>
    <tr>
    <td>Apples <%=i%></td>
    <td><input name="Applesnet<%=i%>" type="text" /></td>
    <td><input name="Applesgross<%=i%>" type="text" /></td>
    <td><input name="Applesdate<%=i%>" type="text" /></td>
    <td><input name="Applestime<%=i%>" type="text" /></td>
    </tr>
    <% Next %>

    <tr>
    <th colspan="5">Orange</th>
    </tr>
    <tr>
    <td>&nbsp;</td>
    <td>net price</td>
    <td>gross price</td>
    <td>date</td>
    <td>time</td>
    </tr>
    <% For i = 1 To iOranges %>
    <tr>
    <td>Apples <%=i%></td>
    <td><input name="Orangesnet<%=i%>" type="text" /></td>
    <td><input name="Orangesgross<%=i%>" type="text" /></td>
    <td><input name="Orangesdate<%=i%>" type="text" /></td>
    <td><input name="Orangestime<%=i%>" type="text" /></td>
    </tr>
    <% Next %>
    </table>
    <input type="submit" />
    </form>
    </body>
    </html>




    --------------------------------------
    INSERT.ASP:
    <%
    Dim iOranges, iApples
    iApples = Request.Form("apples")
    iOranges = Request.Form("oranges")

    Set oADO = CreateObject("ADODB.Connection")
    oADO.Open YourConnectionStringHere
    For i = 1 To iApples
    sSQL = "INSERT INTO [apples] ([net],[gross],[date],[time]) VALUES (" & _
    Request.Form("Applesnet" & i) & "," & _
    Request.Form("Applesgross" & i) & "," & _
    Request.Form("Applesdate" & i) & "," & _
    Request.Form("Applestime" & i) & ")"
    ' oADO.Execute sSQL,,129 UNCOMMENT IF ALL IS FINE
    RESPONSE.WRITE SSQL & "<BR>"
    Next


    For i = 1 To iOranges
    sSQL = "INSERT INTO [Oranges] ([net],[gross],[date],[time]) VALUES (" & _
    Request.Form("Orangesnet" & i) & "," & _
    Request.Form("Orangesgross" & i) & "," & _
    Request.Form("Orangesdate" & i) & "," & _
    Request.Form("Orangestime" & i) & ")"
    ' oADO.Execute sSQL,,129 UNCOMMENT IF ALL IS FINE
    RESPONSE.WRITE SSQL & "<BR>"
    Next
    oADO.Close : Set oADO = Nothing
    %>



    Ray at home


    "Nick" <> wrote in message
    news:...
    > Loop to create an array from a dynamic form.
    >
    > I'm having trouble with an application, and I'll try to explain it as
    > clearly as possible:
    >
    > 1. I have a form with two fields, say Apples and Oranges. The user
    > selects from a drop down menu, between 1-10 of each and clicks submit.
    > The resulting page will display a NEW form, with rows and a list of
    > fields for the amount of each items selected.
    >
    > Example. If I selected 3 apples and 5 oranges, the resulting page
    > would show 3 rows of text fields for apples and 5 rows of text fields
    > for oranges.
    >
    > The form fields would be (after each semicolon, the first word is the
    > form element name (Apple1, Apple2)):
    >
    > Apple1: net price | gross price | date | time
    > Apple2: net price | gross price | date | time
    > Apple3: net price | gross price | date | time
    >
    >
    > Orange1: net price | gross price | date | time
    > Orange2: net price | gross price | date | time
    > Orange3: net price | gross price | date | time
    > Orange4: net price | gross price | date | time
    > Orange5: net price | gross price | date | time
    >
    >
    > I want to insert each one of these records into the database, each
    > record being a net, gross price, date and time for apple 1-X and
    > orange 1-X. The apples will go into the apple table and the oranges
    > will go into the oranges table.
    >
    >
    > I'm used to looping like this:
    >
    > for each item in request.form
    > if left(item, 6) = "orange" then
    > orange = orange & request.form(item) & ","
    > end if
    > next
    >
    >
    > After that, I'll create a loop that inserts each record into the
    > database separately, something of this nature:
    >
    >
    > for each item in orange
    > set rsOrange= server.createobject("adodb.recordset")
    > strSql = "select * from oranges"
    > rsOrange.open strSql, Connstr, 1, 3
    >
    > rsOrange.Addnew
    > rsOrange("net") = request.form("net")
    > rsOrange("gross") = request.form("gross")
    > rsOrange("date") = request.form("date")
    > rsOrange("time") = request.form("time")
    > rsOrange.Update
    > rsOrange.close
    > next
    >
    >
    > But since this is a multi dimensional array (gross, net, date, time
    > will need to be inserted correspondingly), you can see my dilemma. I
    > understand how they work when hard coded, but not to dynamically
    > create and populate them, then use in an insert statement. Sorry if
    > this is elementary, but any help would really be appreciated.
    >
    > Thanks!
    Ray Costanzo [MVP], Aug 26, 2004
    #2
    1. Advertising

  3. Nick

    N I Guest

    Ray
    Thanks so much. I had created the form elements as you described in your
    reply, but your insert loop is perfect and much more simple than what I
    had started working with.


    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
    N I, Aug 26, 2004
    #3
    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. KingdomHeart
    Replies:
    0
    Views:
    429
    KingdomHeart
    Jan 23, 2009
  2. pizzy
    Replies:
    4
    Views:
    138
    pizzy
    Mar 18, 2005
  3. pizzy
    Replies:
    7
    Views:
    154
    pizzy
    Mar 23, 2005
  4. wreed
    Replies:
    9
    Views:
    188
    wreed
    Oct 16, 2006
  5. Isaac Won
    Replies:
    9
    Views:
    365
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page