|
Forms - For Each/Iteration by Charles Carroll
Forms with many elements may be easier to process if a FOR
EACH construct is used to loop through every value submitted from the source form. In this
example, there are many checkboxes and instead of many IF statements we replace them
with one FOR EACH loop.
filename=/learn/test/monthlyform.asp
<html><head>
<TITLE>monthlyForm.asp</TITLE>
</head><body bgcolor="#FFFFFF">
<form action="monthlyFormRespond.asp" method="post">
<p>CheckBox Form Example</p>
<p><b>Check Off The Days You Worked</b></p>
<%
my_month=request.querystring("whichmonth")
' response.write my_month & "<br>"
if my_month="" then
my_date=now()
else
my_month=month(request.querystring("whichmonth") & "/1/2000")
my_date=dateserial(year(now()),my_month,1)
end if
'response.write my_date & "<br>"
If my_month=12 then
my_day=1
my_month=1
my_year=my_year+1
else
my_day=1
my_month=month(my_date)+1
my_year=year(my_date)
end if
lastdayofmonth=day(DateSerial(Year(my_Date), my_Month,0))
'response.write lastdayofmonth & "<br>"
for counter=1 to lastdayofmonth%>
<input TYPE="checkbox" NAME="workingday<%=counter%>">day <%=counter%> Worked?<br>
<%next%>
<input type="submit" value="Submit Days Worked">
</form>
</body></html>
filename=/learn/test/monthlyformrespond.asp
<html><head>
<TITLE>monthlyFormRespond.asp</TITLE>
</head><body bgcolor="#FFFFFF">
<%
daysworked=0
for each whatever in request.form
thisvalue=request.form(whatever)
If thisvalue="on" then
daysworked=daysworked+1
end if
next
response.write "Thanks for working <b>" & daysworked & "</b> days"
%>
</body></html>
|