Click to See Complete Forum and Search --> : Problem retaining values with ASP Dynamic arrays


Iain L
11-16-2005, 06:31 AM
Hi,
I am trying to create a page which will enable visitors to select essay titles from a menu. This form will be Access database driven (the form script below is just a simplified version) but my problem is that I am trying to use a dynamic array to store essays selected by visitors. What happens is whenever someone selects and then selects again, the original selection is lost. So what I need is the script to correct this problem. I am basically at the very edge of my knowledge so any help would be appreciated. The script I have currently worked out is as follows:



<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
<html>
<head>
<title>Essay Selection</title>
</head>

<body>
<p>Session ID:
<%

Session ("visitorID") = session.SessionID
response.write session("visitorID")%>

</p>
<form name="form1" method="post" action="survey2.asp">
<select name="EssayID" size="3" multiple id="EssayID">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>

<input type="submit" name="Submit" value="Submit">
</form>
<p>&nbsp;</p>
<p><br>
<%
Dim EssayID
EssayID = Array(Request.Form("EssayID"))
EssayID= Split( Request.Form("EssayID"), ",")
Response.write("The number of Essays selected is: ")
Response.Write Ubound(EssayID)+1
Redim Preserve Essay(Ubound(EssayID)+1)

%>
</p>
</body>
</html>

Thanks.

Giskard
11-16-2005, 10:22 AM
You need to store the previous selection so that when you repost the page it is passed along with the new choice.

You could do this by marking any previous selections when you load the page:

<select name="EssayID" size="4" multiple id="EssayID">
<option value="1"<% if InStr(Request.form("EssayID"),"1")>0 then Response.write(" Selected")%>>1</option>
<option value="2"<% if InStr(Request.form("EssayID"),"2")>0 then Response.write(" Selected")%>>2</option>
<option value="3"<% if InStr(Request.form("EssayID"),"3")>0 then Response.write(" Selected")%>>3</option>
<option value="4"<% if InStr(Request.form("EssayID"),"4")>0 then Response.write(" Selected")%>>4</option>
</select>



Or you could store the info in a hidden variable (anywhere within the form tag):

<input type=hidden name="EssayID" value="<%=Request.form("EssayID")%>">

The first option gives your user the ability to see the previous selections - depending on your need that could be good or bad.