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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

asp: special form validation problem

Status
Not open for further replies.

selaine

Programmer
Oct 11, 2001
85
US
I'm working with three different asp pages:
Page 1 = Supplier_Reg.asp
Page 2 = Commodities.asp
Page 3 = add_supplier.asp

Page 1 contains part one of a form (textboxes and checkboxes)
I pull all of the textbox information into page 2 as hidden fields as follows:

Response.Write( &quot;<td><input type=&quot;&quot;hidden&quot;&quot; Name=MainCat&quot; & rsMain(&quot;GroupID&quot;) & &quot;></td>&quot; )

The checkboxes created
dynamically from an access database table. The checkboxes are named ck<%=rsConn(&quot;GroupID&quot;)%>,
with the &quot;GroupID&quot; being a key field in the Main Categories table.

Page 2 contains one or more (depending on what checkboxes are ticked on page one) sets of
checkboxes which are created dynamically from an access database table (sub categories), using
the GroupID. Each GroupID (checkbox(s) selected from previous page, develops a list of
checkboxes). As each GroupID is determined, a hidden text field is created on the page so that
I can pass the value to page 3 (name=MainCat&quot; & rsMain(GroupID&quot;). The checkbox sets on this
page are all named catbox<%=rsSub(&quot;CategoryID&quot;)%>.

This is my problem:
I'm not very good at working with loops and need to validate my checkboxes per GroupID.
So if the user selected 3 checkboxes on page 1, there will be 3 GroupIDs to loop thru. Each
GroupID will generate anywhere from 1 - 24 checkboxes from the table. My
sub categories table contains the following fields: &quot;CategoryID&quot;, &quot;GroupID&quot; and &quot;Description&quot;.
I need to loop thru each of the hidden field(s) created on page two that have the
name: MainCat???? (the ???? represent the GroupID to search on) and then check to make sure
at least one of the CategoryID's associated with that particular GroupID have been checked.
Then I need to check the next group of checkboxes. Does anyone know how I would create a loop
to do this? Here is the code I have for the first two pages:

***************************************
Page 1 : category.asp
***************************************
<% @LANGUAGE=VBscript %>
<% Option Explicit %>

<%
Response.Buffer = True

Dim dbConn 'Common Database connection
Dim rsConn 'Common recordset object
Dim strSQL 'SQL string constructor

'Get the database connection
Set dbConn = Server.CreateObject( &quot;ADODB.Connection&quot; )
dbConn.Open &quot;Provider=Microsoft.Jet.OLEDB.4.0;Data Source=&quot; & Server.MapPath( &quot;purchasing.mdb&quot; ) & &quot;;&quot;

'Get the main categories from the (seala.mdb) database; (Main Categories Table)
strSQL = &quot;SELECT GroupID, Description FROM [Main Categories] ORDER BY Description&quot;
Set rsConn = dbConn.Execute( strSQL )
If rsConn.EOF Then
Response.Redirect &quot;MAJORERROR.ASP&quot;
End If

%>
<html>
<head>
<title>Select Main Category</title>
</head>
<body>

<table align=&quot;center&quot;><tbody>
<td colspan=&quot;2&quot; class=&quot;thead&quot;>Select Main Categories</td>
<form name=&quot;maincat&quot; action=&quot;subsection.asp&quot; method=&quot;post&quot;>
<%
'Make our way through the main categories and write the data as we go
While NOT rsConn.EOF
%>
<tr><td><%=rsConn(&quot;Description&quot;)%></td>
<td><input type=&quot;checkbox&quot; name=&quot;chkbox<%=rsConn(&quot;GroupID&quot;)%>&quot;></td>
</tr>
<%
'Next record
rsConn.MoveNext
Wend
%>
<tr><td align=&quot;center&quot; colspan=&quot;2&quot;>
<input type=&quot;submit&quot; value=&quot;Submit Form&quot;> <input type=&quot;reset&quot; value=&quot;Reset Data&quot;>
</td></tr>
</form>

</tbody></table>
</body>
</html>

<%
'Clean up database objects
Set rsConn = Nothing
dbConn.Close
Set dbConn = Nothing
%>

***************************************
Page 2 : subsection.asp
***************************************
<% @LANGUAGE=VBscript %>
<% Option Explicit %>
<html>
<head>
<title>Select sub categories</title>
</head>
<body>
<form action=&quot;catgorysel.asp&quot; method=&quot;post&quot; name=&quot;subcategory&quot;>
<%
Response.Buffer = True

Dim dbConn 'Common Database connection
Dim rsMain 'Common recordset object
Dim rsSub
Dim strSQL 'SQL string constructor
Dim fld
Dim counter

Dim ArrayCheck(100)
Dim ArraySize
ArraySize = 0

'Get the database connection
Set dbConn = Server.CreateObject( &quot;ADODB.Connection&quot; )
dbConn.Open &quot;Provider=Microsoft.Jet.OLEDB.4.0;Data Source=&quot; & Server.MapPath( &quot;purchasing.mdb&quot; ) & &quot;;&quot;

'Get the main categories from the database. All we need at the moment is the identities
strSQL = &quot;SELECT GroupID FROM [Main Categories]&quot;
Set rsMain = dbConn.Execute( strSQL )
If rsMain.EOF Then
Response.Redirect &quot;MAJORERROR.ASP&quot;
End If

'Scan the form looking for checked boxes
While NOT rsMain.EOF
fld = Request.Form( &quot;chkbox&quot; & rsMain(&quot;GroupID&quot;)) 'Get the field from the form
If NOT (fld = False) Then 'Was the box checked
ArrayCheck( ArraySize ) = rsMain(&quot;GroupID&quot;) 'Add the GroupID to the array
ArraySize = ArraySize + 1 'Increment the array size
End If
rsMain.MoveNext 'Next database record
Wend
Set rsMain = Nothing

'Check that something was selected
If ArraySize = 0 Then
Response.Redirect &quot;NOTHINGSELECTED.ASP&quot;
End If

'Make the SQL string for the group selection
strSQL = &quot;SELECT GroupID, Description FROM [Main Categories] WHERE &quot;

'Make our way through the array making the selections we want
For counter = 0 To ArraySize - 1
If counter > 0 Then strSQL = strSQL & &quot; OR &quot; 'Put the OR in if required
strSQL = strSQL & &quot;(GroupID=&quot; & ArrayCheck(counter) & &quot;)&quot;
Next

'Now sort the output
strSQL = strSQL & &quot; ORDER BY Description&quot;

'Get the sorted records we want from the database
Set rsMain = dbConn.Execute( strSQL )
If rsMain.EOF Then
Response.Redirect &quot;MAJORERROR.ASP&quot;
End If

'Start the main category display loop
While NOT rsMain.EOF
'Write the table header
Response.Write( &quot;<table align=&quot;&quot;center&quot;&quot;>&quot; & vbCrLf )

'Write the title of the current selection block (or main category description)
Response.Write( &quot;<tr><td colspan=&quot;&quot;2&quot;&quot;><center><b><font face=&quot;&quot;Arial,Helvetica&quot;&quot; size=+1>&quot; & rsMain(&quot;Description&quot;) & &quot;</font></b></center></td></tr>&quot; & vbCrLf )
Response.Write( &quot;<td><input type=&quot;&quot;hidden&quot;&quot; Name=MainCat&quot; & rsMain(&quot;GroupID&quot;) & &quot;></td>&quot; )

'--------------------------- BEGIN OF SUB-CATEGORY SECTION -------------------------
'Make the SQL string. We need all the categories that belong to the current main group
'and sort them into alphabetical order
strSQL = &quot;SELECT CategoryID, Description FROM [Sub Categories] &quot; & _
&quot;WHERE GroupID=&quot; & rsMain(&quot;GroupID&quot; ) & _
&quot; ORDER BY Description&quot;

'Open the recordset
Set rsSub = dbConn.Execute( strSQL )

'Is there actually anything here
If NOT rsSub.EOF Then

'Start the scan loop
While NOT rsSub.EOF
'This is pretty much the same thing as in category.asp
%>
<tr><td><%=rsSub(&quot;Description&quot;)%></td>
<td><input type=&quot;checkbox&quot; name=&quot;catbox<%=rsSub(&quot;CategoryID&quot;)%>&quot;></td>
</tr>
<%
rsSub.MoveNext %>

<% Wend
End If

'Clean up recordset object
Set rsSub = Nothing

'---------------------------- END OF SUB-CATEGORY SECTION --------------------------
'End of the category table
Response.Write( &quot;</table><br><br>&quot; & vbCrLf )

rsMain.MoveNext
Wend

'--------- Display the submit and reset buttons ------------------------
%>
<div align=&quot;center&quot;>
<input type=&quot;submit&quot; value=&quot;Submit Form&quot;> <input type=&quot;Reset&quot; value=&quot;Reset Form&quot;>
</div>

</form>
</body>
</html>

<%
'Clean up database
dbConn.Close
Set dbConn = Nothing
%>

***************************************
Page 3 : add supplier.asp
***************************************

I know the syntax below isn't even close, but I'm trying to show the basics of what I think needs to happen (using javascript). Most of what I do have I've managed to piece together from various places on the internet and a person named Whitesword was extremely helpful. But, loops just really throw me!

Count of GroupIDs = X
loop X times
for each Name=MainCat&quot; & rsMain(&quot;GroupID&quot;)
go to table and compare catbox<%=rsSub(&quot;CategoryID&quot;)%> with CategoryID in
sub categories and make sure at least one checked
loop to check next GroupID selected on previous page

Help!!! Thanks to all!!!!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top