Current location: Hot Scripts Forums » Programming Languages » ASP » passing array to hidden fields in form on next page?


passing array to hidden fields in form on next page?

Reply
  #1 (permalink)  
Old 09-04-03, 01:15 PM
seala seala is offline
Newbie Coder
 
Join Date: Aug 2003
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
passing array to hidden fields in form on next page?

I'm learning asp and scripting as I go, and don't work with arrays and loops very well yet (They're very confusing! )

Anyway, could someone explain to me how to create an array of checkboxes that were selected from a form on one page and pass them into hidden fields in a form on the next page? I have this massive project I've been working on and am having trouble with this. Thanks!
Reply With Quote
  #2 (permalink)  
Old 09-04-03, 02:04 PM
Shane Shane is offline
Coding Addict
 
Join Date: Jun 2003
Location: Maryland, US
Posts: 268
Thanks: 0
Thanked 0 Times in 0 Posts
Do your checkboxes all have the same name, but with different values?

If so, you can figure out which ones were selected by doing a Request.Form("checkbox_name"). This would return something like:

value1,value2,value3,value4

All you would have to do would be to write that value into the <input type="hidden"> tag.

For example:

Code:
Page.asp - this document contains your checkboxes.

<html>
<body>
<input type="checkbox" name="mybox" value="value1"><br>
<input type="checkbox" name="mybox" value="value2"><br>
<input type="checkbox" name="mybox" value="value3"><br>

</html>


Page2.asp - this document processes your form results and puts the values in a hidden field
<%
Dim strBoxes


strBoxes = Request.Form("mybox")

%>

<input type="hidden" name="checkbox_values" value="<%=strBoxes %>">
__________________
Shane Bauer
Microsoft Certified Professional (MCP) - ASP.NET
ASP/ASP.net, C#, VB/VB.NET, PHP, Perl, SQL
Reply With Quote
  #3 (permalink)  
Old 09-04-03, 02:40 PM
seala seala is offline
Newbie Coder
 
Join Date: Aug 2003
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
The checkboxes are created and named dynamically via an access db - here is my code:

<% @LANGUAGE=VBscript %>
<% Option Explicit %>
<html>
<head>
<title>Select sub categories</title>
</head>
<body>
<form action="catgorysel.asp" method="post" name="subcategory">
<%
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( "ADODB.Connection" )
dbConn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath( "ckbx.mdb" ) & ";"

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

'Scan the form looking for checked boxes
While NOT rsMain.EOF
fld = Request.Form( "ck" & rsMain("GroupID")) 'Get the field from the form
If NOT (fld = False) Then 'Was the box checked
ArrayCheck( ArraySize ) = rsMain("GroupID") '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 "NOTHINGSELECTED.ASP"
End If

'Make the SQL string for the group selection
strSQL = "SELECT GroupID, Description FROM [Main_Categories] WHERE "

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

'Now sort the output
strSQL = strSQL & " ORDER BY Description"

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

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

'Write the title of the current selection block (or main category description)
Response.Write( "<tr><td colspan=""2""><h1>" & rsMain("Description") & "</h1></td></tr>" & vbCrLf )

'--------------------------- 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 = "SELECT CategoryID, Description FROM [Sub_Categories] " & _
"WHERE GroupID=" & rsMain("GroupID" ) & _
" ORDER BY Description"

'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("Description")%></td>
<td><input type="checkbox" name="catbox<%=rsSub("CategoryID")%>"></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( "</table><br><br>" & vbCrLf )

rsMain.MoveNext
Wend

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

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

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


I need to figure out how to take the groupid value array and pass it to the next page to use. Because the user may have selected more than one checkbox from the previous page which then goes into this page, I need to make sure the user selects at least one checkbox on this page per groupid before updated the db. I have no clue how to approach this. The code I have above was pieced together from different sources on the internet, so I'm still confused about working with the arrays and loops. I'd appreciate any help on this, as its a project I've been working on forever and validating the checkbox selections from this page is the next to the last step prior to processing and updating to db. Thanks!
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Formmail Script Fix - Cant display multiple list selections.. zamen PHP 3 05-02-04 09:59 AM
passing values from one form to another -urgent aspuser25 ASP 1 09-19-03 06:41 PM
asp: URGENT! need to change code to create new form per id seala ASP 2 09-09-03 09:54 PM
asp: checkboxes & multi-page form seala ASP 0 09-02-03 01:58 PM
Passing value from one form to another form sasi ASP 2 08-29-03 11:38 PM


All times are GMT -5. The time now is 03:24 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.