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!

pass array values to hidden fields in form on next page?

Status
Not open for further replies.

selaine

Programmer
Oct 11, 2001
85
US
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!
 
Why not just reproduce the entire bunch of checkbox controls <input> elements, in a hidden DIV <div style=&quot;display: none;&quot;><input …/> <input … />… etc</div> inside the form of the new page? That way the will get submitted when the form does.

-pete
 
good tpoic to add to a FAQ but later. [wink]

first thing to get it out of the way and to repeat the FAQ,
if there is no value specified for a checkbox you get this from Request.Form(&quot;myBox&quot;) being checked in your processing script
&quot;on&quot; otherwise you get nothing

now if you have a value attributed to the checkbox (eg:<checkbox value=&quot;some&quot;> you'll get with Request.Form(&quot;myBox&quot;) &quot;some&quot; or nothing

now that was singled out checkbox's. your array will be created for you after a checkbox group has been ineracted with.
eg:
<checkbox name=&quot;chk&quot; value=&quot;some&quot;>
<checkbox name=&quot;chk&quot; value=&quot;some&quot;>
<checkbox name=&quot;chk&quot; value=&quot;some&quot;>
<checkbox name=&quot;chk&quot; value=&quot;some&quot;>
<checkbox name=&quot;chk&quot; value=&quot;some&quot;>

processing that is simple
for each chkItem in request.form
response.write Checkbox was checked with value of & &quot;: &quot; & request.form(chkItem) & &quot;<br>&quot;
next

there's nothing else to it really

_____________________________________________________________________
Where have all my friends gone to????
onpnt2.gif

 
The problem is that the checkboxes are created dynamically Here is my code....

<% @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;ckbx.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;ck&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;><h1>&quot; & rsMain(&quot;Description&quot;) & &quot;</h1></td></tr>&quot; & 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 = &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
%>
 
btw: the hidden field thing

this has nothing to do with the checkbox's
you need to learn to take your process and structure it into a way you have
situation 1
situation 2

hence what psuedocode was invented for

eg:
form gets submited
(normaly you would have you form tags here)

form submitted
call asp script

get values of checkbox's
loop through collection
for each checkbox in checkboxs
write out hidden field

(write syntax)
<input type=&quot;hidden&quot; value=&quot;checkbox value passed&quot; name=&quot;&quot;>
next

now you use that to write your code

for each chkItem in request.form
response.write &quot;<input type=&quot;&quot;hidden&quot;&quot;&quot;
response.write &quot; value='&quot; & request.form(chkItem) & &quot;' &quot;
response.write &quot;name=&quot;&quot;chkPassed&quot;&quot;>&quot;
next



_____________________________________________________________________
Where have all my friends gone to????
onpnt2.gif

 
then you have unique checkbox's and you have no concern with array. This is not a group but singled objects in the form

I would get the values passed (or more to the point the name) with a InStr()
eg: (in the loop)

If InStr(Request.Form(Item),&quot;catbox&quot;) > 0 Then
... I have my value ...
Response.Write hidden syntax & Request.Form(Item)


_____________________________________________________________________
Where have all my friends gone to????
onpnt2.gif

 
Perahps your problem would be greatly simplified if you changed this:
<td><input type=&quot;checkbox&quot; name=&quot;catbox<%=rsSub(&quot;CategoryID&quot;)%>&quot;></td>

to this:
Code:
<td><input type=&quot;checkbox&quot; name=&quot;catbox&quot; value=&quot;<%=rsSub(&quot;CategoryID&quot;)%>&quot;></td>
&quot;But, that's just my opinion... I could be wrong.&quot;

-pete
 
I've implemented a similar thing where I dynamically generate my checkboxes based on a table in my database:

On the first page.asp, you would have this code to dynamically create your checkboxes:
Code:
<td><input type=&quot;checkbox&quot; name=&quot;chk<%=rsSub(&quot;CatID&quot;)%>&quot; value=checked></td>

...and since I know that the names of the check boxes come from a table, I open the same &quot;rsSub&quot; in the next form to read the values of the checkboxes...

On the second page (action of the first page's form)
Code:
<% 
  do while not rsSub.eof 
    if request.form(&quot;chk&quot; & rsSub(&quot;CatID&quot;)) = &quot;checked&quot; then
%>
Code:
    store that value or whatever you plan to do
Code:
<% 
    rsSub.movenext
  loop 
%>


Earnie Eng
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top