Search Search

#1 worldwide
FREE Coding Lessons

since 1996
   THE BEST WAY to learn ASP & Asp.net!
Advertise Here!
click for details
Credits Host:
DiscountASP.net
Server Admin:
The "Team"
Contact Info.
Charles M. Carroll

my Blog
[prev. Lesson]  Forms: IF syntax #4
     [next Lesson]  Form - Submit To Self'

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

<Test Script Below>


<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

<Test Script Below>


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

Chaz Wish List
Tall Tip $5
Grande Tip $20
Venti Tip $39
Tip Jar Thanks
2004 Thanks
2005 Thanks
HUGE Tip -love site