Retrieving values of dynamic lists

A

Andyza

In the code from page test1.asp below I retrieve some records from a
database and create a list of classes available to users. The list
contains the names of the classes with a checkbox next to each class
so that users can select the classes that they want to attend. There
is also a drop-down list next to each class name displaying the dates
that the class will be held.

On test2.asp I retrieve the names of the classes that have been
selected (checkboxes that have been selected) using:
Request.Form("chkClassName"). No problem so far.

I also want to get the DATE that has been selected by the user for
each of the classes that he has selected. I tried retrieving this info
on test2.asp using Request.Form("lstClass") but
Request.Form("lstClass") contains the dates from ALL the drop-down
lists on test1.asp instead of just the dates from the drop-down lists
for the specific classes that the user selected.

For example, if there are 4 classes on the list and the user selects 2
classes then:

Request.Form("chkClassName") contains just the names of the 2 classes
that the user selected - which is what I want. BUT,
Request.Form("lstClass") contains the dates for ALL 4 of the classes
on the list instead of just the dates for the 2 classes that the user
selected. I only want the dates for the 2 classes that the user
selected.

Is there a way to do this?

test1.asp is:

<% @ Language=VBScript %>
<%
Dim conn, sSQL, rs1, rs2

Set conn = Server.CreateObject("ADODB.Connection")
conn.Open Application("ConnString")
%>
<html>
<body>
<form name="frmClassSelection" method="post" action="test2.asp">
<table border="0">
<%
sSQL1 = "SELECT CourseID, CourseName FROM tblClasses ORDER BY
CourseName"
Set rs1 = conn.Execute(sSQL1)
Do While Not rs1.eof
sSQL2 = "SELECT CourseDate FROM tblClassDates WHERE CourseID =
'" & rs1("CourseID") & "' ORDER BY CourseDate"
Set rs2 = conn.Execute(sSQL2)
If rs2.eof Then
%>
<tr>
<td><input type="checkbox" name="chkClassName" value="<
%=rs1("CourseID")%>" disabled> (CourseID: <%=rs1("CourseID")%>)</td>
<td><%=rs1("CourseName")%></td>
<td>No dates available</td>
</tr>
<%
Else
%>
<tr>
<td><input type="checkbox" name="chkClassName" value="<
%=rs1("CourseID")%>"></td>
<td><%=rs1("CourseName")%></td>
<td>
<select name="lstClass">
<%
Do While Not rs2.eof
%>
<option name="optDate<%=rs1("CourseID")%>" value="<
%=rs2("CourseDate")%>"><%=rs2("CourseDate")%></option>
<%
rs2.MoveNext()
Loop
%>
</select>
</td>
</tr>
<%
End If
rs2.Close
Set rs2 = nothing
rs1.MoveNext()
Loop
rs1.Close
Set rs1 = nothing
%>
<tr>
<td colspan="3">
<input type="submit" name="btnSubmit" value="Submit">
</td>
</tr>
</table>
</form>
</body>
</html>
<%
conn.Close
Set conn = nothing
%>

test2.asp contains just a test page with a bunch of response.write
debug statements and a database Insert statement.

I'm using a MSSQL 2005 database and IIS and yes, I will move the sql
statements into stored procedures once I've figured out how to get the
dates from the lists!

;->

Thanks.
 
B

Brynn

Hi,

I program with mostly C# for my web apps now, but took a stroll down
memory lane and came back to my classic asp buds for some fun.

Without going crazy and turning this into subroutines and getrows, and
blah blah blah ... I just added a tiny little change that you will be
able to easily see and use with very little change to "page2.asp" code
on your end. I added x as a counter for your checkboxes and
dropdowns ... where there is changed code ... there is a comment like
this *** A comment like this *** Lets look at page1.asp ... and a
quickie 10 lines of what your Page2.asp will include.

==========
Page1.asp
==========

<% @ Language=VBScript %>
<%
Dim conn, sSQL, rs1, rs2

Set conn = Server.CreateObject("ADODB.Connection")
conn.Open Application("ConnString")
%>
<html>
<body>
<form name="frmClassSelection" method="post" action="test2.asp">
<table border="0">
<%
sSQL1 = "SELECT CourseID, CourseName FROM tblClasses ORDER BY
CourseName"
Set rs1 = conn.Execute(sSQL1)

'// ********* add a counter *********
dim x
x = 0

Do While Not rs1.eof
sSQL2 = "SELECT CourseDate FROM tblClassDates WHERE CourseID ='"
& rs1("CourseID") & "' ORDER BY CourseDate"
Set rs2 = conn.Execute(sSQL2)
If rs2.eof Then
%>
<tr>
<td><input type="checkbox" name="chkClassName" value="<
%=rs1("CourseID")%>" disabled> (CourseID: <%=rs1("CourseID")%>)</td>
<td><%=rs1("CourseName")%></td>
<td>No dates available</td>
</tr>
<%
Else
x=x+1 '// ****** add to counter for each school course ******
%>
<tr>
<!-- ********* use counter to give your checkboxes UNIQUE NAMES
dynamically chkClassName1, chkClassName2, etc (( I also added a hidden
field to pass the course ID ********* -->
<td><input type="checkbox" name="chkClassName<% =x %>"
value="<%=rs1("CourseID")%>">
<input type="hidden" name="courseID<% =x %>" value="<
%=rs1("CourseID")%>"/>
</td>

<td><%=rs1("CourseName")%></td>
<td>
<!-- ********* use counter to give your dropdowns UNIQUE NAMES
dynamically ********* -->
<select name="lstClass<% =x %>">

<%
Do While Not rs2.eof
%>
<option name="optDate<%=rs1("CourseID")%>" value="<
%=rs2("CourseDate")%>"><%=rs2("CourseDate")%></option>
<%
rs2.MoveNext()
Loop
%>
</select>
</td>
</tr>
<%
End If
rs2.Close
Set rs2 = nothing
rs1.MoveNext()
Loop
rs1.Close
Set rs1 = nothing
%>
<tr>
<td colspan="3">

<!-- ********* use the counter and create a hidden field in your
form
to pass the count to your page2.asp ... use this
number on page2 ********* -->
<input type="hidden" name="fieldCount" value="<% =x %>" />

<input type="submit" name="btnSubmit" value="Submit">
</td>
</tr>
</table>
</form>
</body>
</html>
<%
conn.Close
Set conn = nothing
%>


==========
Page2.asp
==========

Now when you get to page 2, it is going to be simple.
Just loop through the total number of courses on the previous page.
Figure out which ones were chosen, and only select those, and their
dates.

<%
'// Get your field count from the number of courses.
dim myCounter
myCounter = request.form("fieldCount")

dim classNameCheckbox, courseID, classDate

For x = 0 to myCounter
classNameCheckbox = request.form("chkClassName" & x) '// x for the
number on the form

if classNameCheckbox = "on" then '// Then it is a chosen course
get the data and process
courseID = request.form("courseID" & x)
classDate = request.form("lstClass" & x)

'additional code to add the course chosen to your list, data,
whatever whatever
end if
Next

%>


See ... the x counter was passed from the one form to this page.
Each record will be handled independently of each other giving you a
lot more options for entering the data on your processing page ...
creating a confirmation page ... whatever.

Take care.
 

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,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top