Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with Form Checkbox (Multiple Checkbox with Same Name)

Status
Not open for further replies.

rleffew1

Vendor
Oct 20, 2003
8
US
I have seen where this has been sort of asked in other threads. But couldnt find a difinitve answer.

I have created a form with a table inside.

The table is populated by a Access database based on User input from a previous page. I don't know how many rows of data because this will depend on how many students are in a class.

The table has three elements I care about.

1) studentID (Hidden Field)
2) chkAttend (checkbox named chkAttend)
3) chkPLUCredit (checkbox name chkPLUCredit)

The user can check one or both or none of the checkboxes per student.

This means I will have multiple studentID's, multiple chkattends and multiple chkPlucredits.

After the user is thru checking checkboxes I submit the form.

I need to then read the form and update my master file with the checkboxes that have been checked. The hidden field within the form is the StudentID I use to pull the appropriate record. What I cant figure out how to do this is since I dont have unique names for the checkboxes per student how can I loop thru the form.for each item in request.fields

You can see what I am doing at this url. If you click submit I display the form. As you can see I get the results , just dont know how to loop thru each element.



Thanks,

Bob Leffew
 
This isnt going to work the way the page is set up.

All of your hidden fields are named xhdnStudentID and they will all come over in the Request as a list...

So if you do:
Response.Write Request("xhdnStudentID")

You'll get something like this:
559,701,1029,1166,1972

Except they are not guaranteed to always in in that order. Different browsers may send them in different orders. Different versions of teh same browser may also.

The check boxes will also come across in a list, but only those that are actually checked.

So if three students attend.. and you do this:
Response.Write Request.Form("ChkAttend")

You'll get this:
Yes,Yes,Yes

But you have no way of knowing WHICH three students attended.




 
Do you have another approach I could take. The only way I see how to do this is simply pull up every student master file and update from there. I was trying to keep from doing this.

But you are right, the dilema has been the check boxes corresponding to the student ids. Is there a way to do this in javascript. But then how would I use the data to read my database which I would have to do in ASP.

Thanks,

Bob Leffew
 
First, get rid of the hidden fields.

Second, incorporate the Student ID as the value of the checkboxes.

So that if you do this:
Response.Write Request.Form("chkAttend")

You get this:
559, 1029, 1972

... assuming those 3 checkboxes were checked.
 
Then it is just a matter of dealing with the string of IDs.

You could use the Split() function to break it into an array with the comma as the delimmitter... might be comma plus space... should test it.

Anyway, something like this:

MyArray = Split(Request.Form("chkAttend"), ",")

Then you could use a For/Next loop like this:
For i = LBound(MyArray) To UBound(MyArray)
Response.Write "This one attended: " & MyArray(i) & "<BR>"
Next


... except instead of doing the Response.Write you would update your database or whatever functionality you need.
 
Here, I made a couple of test pages for you to demonstrate:

This is the html form page:
Code:
<html>
<head>
<title>foo test</title>
</head>
<body>
<form method=post action=process.asp>
 Click box for favorite types of pet:
 <br>
 <br>
 dog <input type="checkbox" name="pet" value="dog">
 <br>
 cat <input type="checkbox" name="pet" value="cat">
 <br>
 bird <input type="checkbox" name="pet" value="bird">
 <br>
 fish <input type="checkbox" name="pet" value="fish">
 <br>
 smurf <input type="checkbox" name="pet" value="smurf">
 <br>
 ape <input type="checkbox" name="pet" value="ape">
 <br>
 <br>
 <input type="submit">
</form>
</body>
</html>


This is the ASP that processes the form:
Code:
<%
For each foo in Request.Form
  Response.Write foo & " = " & Request.Form(foo) & "<BR>"
Next

Response.Write "<BR><BR>" & vbCrlf

MyArray = Split(Request.Form("pet") , ",")
For i = LBound(MyArray) To UBound(MyArray)
  Response.Write "You might like a pet " & MyArray(i) & ".<BR>"
Next
%>

This should get you started.

The difference in that in yours you will be setting the checkbox values using ASP but in this example they are hard coded.
 
you can also do

Code:
for each pet in request.form("pet")
  Response.Write "You might like a pet " & pet & ".<BR>"
Next
 
Thanks for everyones help.

I read up some and figured out the following solution
*****************************************************
Code for building multiple checkboxes with same name.
*****************************************************
If (Not MyRs.BOF) AND (Not MyRs.EOF) Then

MyRs.MoveFirst

Response.Write ("<b>Number Of Participants: ") & MyRs.RecordCount & "</b><BR><BR>"

Response.Write "<HR><Table Align='Center' valign='top' border='0' width='82%%' bgcolor='AACCFF'><TR>"
Response.Write "<TD Align='Left' valign='Middle' border='0' width='45%'><B>Student</B></TD>"
Response.Write "<TD Align='Left' valign='Middle' border='0' width='28%'><B>Attended?</B></TD>"
Response.Write "<TD Align='Left' valign='Middle' border='0' width='27%'><B>Earned Credits?</B></TD>"
Response.Write "</TR></Table><HR>"

Do While Not MyRs.EOF

StudentName = Trim(MyRs.Fields("FirstName") & " " & MyRs.Fields("Initial"))
StudentName = StudentName & " " & MyRs.Fields("LastName")
ClassID = MyRs.Fields("ClassID")
StudentID = MyRs.Fields("StudentID")
strAttendedBoxNames = strAttendedBoxnames & "AT" & MyRs.Fields("StudentClassID") & ","
strCreditEarnedBoxNames = strCreditEarnedBoxnames & "CE" & MyRs.Fields("StudentClassID") & ","

If MyRs.Fields("InstructorAttended") = True Then
AttendedIDBox = "<INPUT TYPE='checkbox' Value='YES' NAME='AT" & MyRs.Fields("StudentClassID") & "' Checked>"
Else
AttendedIDBox = "<INPUT TYPE='checkbox' Value='YES' NAME='AT" & MyRs.Fields("StudentClassID") & "'>"
End If

If MyRs.Fields("InstructorCreditEarned") = True Then
CreditEarnedIDBox = "<INPUT TYPE='checkbox' Value='YES' NAME='CE" & MyRs.Fields("StudentClassID") & "' Checked>"
Else
CreditEarnedIDBox = "<INPUT TYPE='checkbox' Value='YES' NAME='CE" & MyRs.Fields("StudentClassID") & "'>"
End If
%>

<Table width="82%" height="22" border="1" Align="Center" cellpadding="0" cellspacing="0" bgcolor="AACCFF" valign="top"><TR>
<TD width="45%" height="20" Align="Left" valign="Middle" border="0"><span Class="style9"><%=StudentName%><BR></span></TD>
<TD Align="Left" valign="Middle" border="0" width="28%"><span class="style9"><%Response.Write "Attended: " & AttendedIDBox%><BR></span></TD>
<TD Align="Left" valign="Middle" border="0" width="27%"><%Response.Write "Credit Earned: " & CreditEarnedIDBox%>&nbsp;</TD>
</TR>
</Table>
<%
MyRs.MoveNext
Loop

MyRs.Close
MyConn.Close
Set MyRs = Nothing
Set MyConn = Nothing

If Right(strAttendedBoxNames, 1) = "," Then
strAttendedBoxNames = Left(strAttendedBoxNames, (Len(strAttendedBoxNames) - 1))
End If

If Right(strCreditEarnedBoxNames, 1) = "," Then
strCreditEarnedBoxNames = Left(strCreditEarnedBoxNames, (Len(strCreditEarnedBoxNames) - 1))
End If

Session("AttendedBoxNames") = strAttendedBoxNames
Session("CreditEarnedBoxNames") = strCreditEarnedBoxNames

************************************************************
CODE FOR RETRIEVING CHECKBOXES:
************************************************************If
IF Flag = "1" Then

checkID = Request.Form("txtID")
strAttendedBoxNames = Session("AttendedBoxNames")
strCreditEarnedBoxNames = Session("CreditEarnedBoxNames")

arrAttendedBoxNames = Split(strAttendedBoxNames, ",", -1, 1)
arrCreditEarnedBoxNames = Split(strCreditEarnedBoxNames, ",", -1, 1)

DBPath = Server.MapPath("/database/GVSDCMast.mdb")
Set MyConn = Server.CreateObject("ADODB.Connection")
MyConn.Open "Driver={Microsoft Access Driver (*.mdb)};DBQ=" & DBPath

If Len(arrAttendedBoxnames(0)) > 2 Then

For I = LBound(arrAttendedBoxNames) To UBound(arrAttendedBoxNames)

isAttendedChecked = ""
isCreditEarnedChecked = ""

isAttendedChecked = Request.Form(arrAttendedBoxNames(I))
isCreditEarnedChecked = Request.Form(arrCreditEarnedBoxNames(I))

SQL = "SELECT * FROM StudentClass Where StudentClassID = " & CLng(Right(arrAttendedBoxnames(I), (Len(arrAttendedBoxnames(I)) - 2)))
Set MyRs = Server.CreateObject("ADODB.Recordset")
MyRs.Open SQL, MyConn, adOpenKeyset, adLockPessimistic, adCmdText
response.Write isDeleteChecked

If (isAttendedChecked = "YES") Then
MyRs.Fields("InstructorAttended") = True
Else
MyRs.Fields("InstructorAttended") = False
End If
If (isCreditEarnedChecked = "YES") Then
MyRs.Fields("InstructorCreditEarned") = True
Else
MyRs.Fields("InstructorCreditEarned") = False
End If

MyRs.Update
MyRs.Close
Set MyRs = Nothing

Next
End If

MyConn.Close
Set MyConn = Nothing
IF Flag = "1" Then

checkID = Request.Form("txtID")
strAttendedBoxNames = Session("AttendedBoxNames")
strCreditEarnedBoxNames = Session("CreditEarnedBoxNames")

arrAttendedBoxNames = Split(strAttendedBoxNames, ",", -1, 1)
arrCreditEarnedBoxNames = Split(strCreditEarnedBoxNames, ",", -1, 1)

DBPath = Server.MapPath("/database/GVSDCMast.mdb")
Set MyConn = Server.CreateObject("ADODB.Connection")
MyConn.Open "Driver={Microsoft Access Driver (*.mdb)};DBQ=" & DBPath

If Len(arrAttendedBoxnames(0)) > 2 Then

For I = LBound(arrAttendedBoxNames) To UBound(arrAttendedBoxNames)

isAttendedChecked = ""
isCreditEarnedChecked = ""

isAttendedChecked = Request.Form(arrAttendedBoxNames(I))
isCreditEarnedChecked = Request.Form(arrCreditEarnedBoxNames(I))

SQL = "SELECT * FROM StudentClass Where StudentClassID = " & CLng(Right(arrAttendedBoxnames(I), (Len(arrAttendedBoxnames(I)) - 2)))
Set MyRs = Server.CreateObject("ADODB.Recordset")
MyRs.Open SQL, MyConn, adOpenKeyset, adLockPessimistic, adCmdText
response.Write isDeleteChecked

If (isAttendedChecked = "YES") Then
MyRs.Fields("InstructorAttended") = True
Else
MyRs.Fields("InstructorAttended") = False
End If
If (isCreditEarnedChecked = "YES") Then
MyRs.Fields("InstructorCreditEarned") = True
Else
MyRs.Fields("InstructorCreditEarned") = False
End If

MyRs.Update

MyRs.Close
Set MyRs = Nothing

Next
End If

MyConn.Close
Set MyConn = Nothing
%>
 
Ah so you incorporated the student info into the NAME property instead of the VALUE?
 
Thanks Sheco,

Your following code got me in the right direction.

I appreciate yours and everyones help.

MyArray = Split(Request.Form("pet") , ",")
For i = LBound(MyArray) To UBound(MyArray)
Response.Write "You might like a pet " & MyArray(i) & ".<BR>"
Next

Bob Leffew
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top