Loop to create an array from a dynamic form.

N

Nick

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!
 
R

Ray Costanzo [MVP]

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
 
N

N I

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.
 

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,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top