469,625 Members | 1,074 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,625 developers. It's quick & easy.

Looping through form elements

Hello,

I have a form that loops through a set of questions pulled from a database
and creates text boxes for the answers to the quesetions. I don't know
before hand how many questions there will be. I can create the text box and
name it (Answer01, 02, 03 ect.), please see code below. The question I
have, is how do I loop through the number of text boxes when I submit the
form to another asp page? For example, if I have 20 text boxes (Answer20),
how do I know that "Answer20" is the last text box?

--
Thanks in advance,

Steven

<div align="left">
<table border="0" cellpadding="2" cellspacing="0" width="600"
bordercolor="#000000">

<% Do While Not spSearch.EOF
If CInt(spSearch("strQuestionNo")) < 10 Then
strParm10 = strQuote & "Answer0" & spSearch("strQuestionNo") & strQuote
Else
strParm10 = strQuote & "Answer" & spSearch("strQuestionNo") & strQuote
End If
%>
<tr>
<td width="40" valign="top" class="sckArial_BlkB08"><div
align="center"><%= spSearch("strQuestionNo")%></div></td>
<td width="560" class="sckArial_BlkM08"><div align="left"><%=
spSearch("strQuestion")%></div></td>
</tr>
<% spSearch.MoveNext %>
<% Loop %>
Jul 19 '05 #1
3 8339
> I have a form that loops through a set of questions pulled from a database
and creates text boxes for the answers to the quesetions. I don't know
before hand how many questions there will be. I can create the text box and name it (Answer01, 02, 03 ect.), please see code below. The question I
have, is how do I loop through the number of text boxes when I submit the
form to another asp page? For example, if I have 20 text boxes (Answer20), how do I know that "Answer20" is the last text box?


You could loop through everything in the form collection and only grab those
that contain "Answer" in their names...

for each x in request.form
if lcase(x, 6) = "answer" then
response.write request.form(x) & "<br>"
end if
next

Or, something like that.

Randy
Jul 19 '05 #2
At the time of writing the questions on the page
you can write the no of total questions to a hidden variable.

Also use the same control name for all questions.

After submitting you can
loop through this control array.
"Steven K" <sk****@troop.com> wrote in message
news:e$***************@tk2msftngp13.phx.gbl...
Hello,

I have a form that loops through a set of questions pulled from a database
and creates text boxes for the answers to the quesetions. I don't know
before hand how many questions there will be. I can create the text box and name it (Answer01, 02, 03 ect.), please see code below. The question I
have, is how do I loop through the number of text boxes when I submit the
form to another asp page? For example, if I have 20 text boxes (Answer20), how do I know that "Answer20" is the last text box?

--
Thanks in advance,

Steven

<div align="left">
<table border="0" cellpadding="2" cellspacing="0" width="600"
bordercolor="#000000">

<% Do While Not spSearch.EOF
If CInt(spSearch("strQuestionNo")) < 10 Then
strParm10 = strQuote & "Answer0" & spSearch("strQuestionNo") & strQuote
Else
strParm10 = strQuote & "Answer" & spSearch("strQuestionNo") & strQuote
End If
%>
<tr>
<td width="40" valign="top" class="sckArial_BlkB08"><div
align="center"><%= spSearch("strQuestionNo")%></div></td>
<td width="560" class="sckArial_BlkM08"><div align="left"><%=
spSearch("strQuestion")%></div></td>
</tr>
<% spSearch.MoveNext %>
<% Loop %>

Jul 19 '05 #3
> for each x in request.form
if lcase(x, 6) = "answer" then
response.write request.form(x) & "<br>"
end if
next


Son of a! That should be...

for each x in request.form
if left(lcase(x), 6) = "answer" then
response.write request.form(x) & "<br>"
end if
next
Jul 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Anna | last post: by
5 posts views Thread by Richard Cornford | last post: by
1 post views Thread by Howard Jess | last post: by
12 posts views Thread by Pudlik, Szymon | last post: by
2 posts views Thread by Brett | last post: by
25 posts views Thread by Dave | last post: by
1 post views Thread by Mel | last post: by
2 posts views Thread by DoomedLung | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.