[thelist] VBScript - declaring dynamically named vars in a Fo r loop
Brian Cummiskey
Brian at hondaswap.com
Thu Mar 3 14:11:16 CST 2005
Pringle, Ron wrote:
> I've already sussed that part out, creating the dynamic form fields
> themselves.
ok cool...
> What I'm attempting to do now is read in those dynamic fields,
> create the appropriate number of vars to hold the contents of each field,
> and then check those fields for errors before submitting the data to a
> database.
here's some more code :)
<%
' ok, before we do anything, error-check the input data and re-direct
back if necessary.
dim numentries, errormsg, errorcount
if isNumeric(Request("numEntries")) then
numEntries = CInt(Request("numEntries"))
else
numEntries = 0
end if
errorcount = 0
errorcount = CInt(errorcount)
for i = 1 to numEntries step 1
response.write "<input type='hidden' name='field1" & i & "' value='" &
request("field1"& i) & "' />" & vbcrlf
response.write "<input type='hidden' name='field4" & i & "' value='" &
request("field4"& i) & "' />" & vbcrlf
response.write "<input type='hidden' name='mymonth" & i & "' value='" &
request("mymonth"& i) & "' />" & vbcrlf
response.write "<input type='hidden' name='myyear" & i & "' value='" &
request("myyear"& i) & "' />" & vbcrlf
if request("field1" & i) = "" then
errormsg = errormsg & "<li>A <em>Name</em> is required for row " & i &
"</li>" & vbcrlf
errorcount = errorcount + 1
response.write "<input type='hidden' name='errorfield1" & i & "'
value='" & cint(1) &"' />" & vbcrlf
end if
if (len(request("field4" & i)) < 10) OR (not
(isNumeric(request("field4" & i)))) then
errormsg = errormsg & "<li>A Valid <em>Phone Number</em> is required
for row " & i & "</li>" & vbcrlf
errorcount = errorcount + 1
response.write "<input type='hidden' name='errorfield4" & i & "'
value='" & cint(1) &"' />" & vbcrlf
end if
if (len(request("mymonth" & i)) < 2) OR (not
(isNumeric(request("mymonth" & i)))) then ' OR (request("mymonth" & i) >
12) then
errormsg = errormsg & "<li>A Valid <em>Month</em> is required for row
" & i & "</li>" & vbcrlf
errorcount = errorcount + 1
response.write "<input type='hidden' name='errormymonth" & i & "'
value='" & cint(1) &"' />" & vbcrlf
end if
if (len(request("myyear" & i)) < 2) OR (not (isNumeric(request("myyear"
& i)))) then 'OR (request("myyear" & i) > 05) then
errormsg = errormsg & "<li>A Valid <em>Year</em> is required for row "
& i & "</li>" & vbcrlf
errorcount = errorcount + 1
response.write "<input type='hidden' name='errormyyear" & i & "'
value='" & cint(1) &"' />" & vbcrlf
end if
errormsg = errormsg & "</ul><ul>" & vbcrlf 'creates a list break in
between rows of errors
next
errorcount = CInt(errorcount)
if errorcount > 0 then
%>
<input type='hidden' name='errormsg' value='<%= errormsg %>' /> & vbcrlf
<input type='hidden' name='errorcount' value='<%= errorcount %>' /> &
vbcrlf
</form></div>
<%
response.write "<script type='text/javascript'>" & vbcrlf
response.write "document.redirectme.submit();" & vbcrlf
response.write "</script>" & vbcrlf
else
response.write "</form></div>" & vbcrlf
'--------------------------- end error check/submit back to form (i sent
you this code yesteryday i think) -----------------------
'---- and now, for the loop ----------------
' do some stored procedure connection here.... then..
for i = 1 to CInt(Request.Form.Item("numEntries")) step 1
field1 = "field1" & i
field2 = "field2" & i
field3 = "field3" & i
field4 = "field4" & i
field5 = "field5" & i
field6 = "myyear" & i
field7 = "mymonth" & i
theyear = CInt("20" & Request.Form(field6))
themonth = CInt(Request.form(field7))
enteredDate = "20" & Request.Form(field6) & Request.Form(field7)
call getBeforeAfter(theyear,themonth)
varFields =
Array("name","firstName","lastName","phone","subID","qDate","qNDate","qPDate")
varValues =
Array(Request.Form(field1),Request.Form(field2),Request.Form(field3),Request.Form(field4),Request.Form(field5),enteredDate,nextDate,priorDate)
oRs.AddNew varFields,varValues
oRs.Update
next
oRs.Close
%>
<div id="header">
<h1>Phone Data Reports</h1>
</div>
<div id="menu">
<a href="default.asp">Return to Main Menu</a>
</div>
<div id="content">
<h5> records isnerted</h5>
</div>
</body>
</html>
<%
' end the re-direct if loop
end if
%>
More information about the thelist
mailing list