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

fill option list with array

Status
Not open for further replies.

Alabaster

Programmer
Aug 11, 2001
44
0
0
CA
I need to fill an option list with an array and the only thing I've been able to find is to use javascript, except I can not get js to use my array made in vbs.
can someone please help or point me in a direction that will work. The following is my code so far.

Thanks in advance
Peter X-)


<title>Test page</title>
<script language=&quot;javascript&quot;>
function clearList(list)
{
var i = 0;
var o = list.options;

for (i = o.length; i >= 0; --i)
o = null;
list.disabled = true;
}


function addElement(list, text_in, value_in)
{
var o = list.options;
var nIdx;
if (o.length < 0) //IE for Mac 4.5 sets length to -1 if list is empty
nIdx = 0;
else
nIdx = o.length;

o[nIdx] = new Option(text_in, value_in);
}

</script>
</head>

<body>
<form name=&quot;storysubmit&quot;>
<table border=&quot;0&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; width=&quot;100%&quot;>
<tr>
<td width=&quot;31%&quot;><select size=&quot;1&quot; name=&quot;neNetType&quot; onChange=&quot;GetTopics(this);&quot;>
<option></option>
<option value=&quot;Agnet&quot;>Agnet</option>
<option value=&quot;FSNet&quot;>FSNet</option>
<option value=&quot;AnimalNet&quot;>AnimalNet</option>
<option value=&quot;BioEdNet&quot;>BioEdNet</option>
</select></td>
<td width=&quot;69%&quot;></td>
</tr>
<SCRIPT LANGUAGE=&quot;JavaScript&quot;>
function GetTopics(SelectNet){
<% 'open the data connection %>
<!--#include file=&quot;./opendatabase.asp&quot;-->
<% dim arrTopics
'SQL query to the database...
SQL = &quot;SELECT Topic FROM Topics&quot; %>
<!--#include file=&quot;./opensql.asp&quot;-->
<% if not rs.eof then
arrTopics = rs.getrows()
arrTopicsCount = Ubound(arrTopics)
Else
arrTopicsCount = -1
End IF
%>

clearList(document.storysubmit.neTopic);
addElement(document.storysubmit.neTopic, arrTopics, arrTopics);
}
</script>

<tr>
<td width=&quot;31%&quot;><select size=&quot;1&quot; name=&quot;neTopic&quot;>
<option value=&quot;-1&quot;>Choose Net Type</option>
</select></td>
<td width=&quot;69%&quot;></td>
</tr>
<tr>
<td width=&quot;31%&quot;></td>
<td width=&quot;69%&quot;><input type=&quot;submit&quot; value=&quot;Submit&quot; name=&quot;B1&quot;><input type=&quot;reset&quot; value=&quot;Reset&quot; name=&quot;B2&quot;></td>
</tr>
</table>
</form>

</body>

</html>
 
Pardon me for switching to VBScript on the server side but why don't you loop through the list and build the HTML when you get the RS instead of getting the Array and then pushing it through JS? I am not sure of the equivalent loop in JS...

example:

<% dim arrTopics
'SQL query to the database...
SQL = &quot;SELECT Topic FROM Topics&quot; %>

<!--#include file=&quot;./opensql.asp&quot;-->

<% if not rs.eof then
arrTopics = rs.getrows()
arrTopicsCount = Ubound(arrTopics)
Else
arrTopicsCount = -1
End IF


strOut = &quot;&quot;

For i=0 to arrTopicsCount

strOut = strOut & &quot;<option value='&quot;& arrTopics(0,i) _
& &quot;'>&quot;& arrTopics(1,i) & &quot;</option>&quot;

Next

Response.Write strOut

%>
 
I don't think that will work in a client-side script. I need this second list to populate &quot;on change&quot; of the first list.

but thanks for the idea.

Peter
 
Opps, It seems I may have hacked out a line when I was pasting the code, The end result should be a for(net in Topics) so that the for only executes on items in my array that match the net. (the net is in the value of each item in the topics)

I'm sorry if I've confused every one. The objective of this script is to filter the second list based on the first list value, but the data for the second list must come from the SQL.

Peter s-)
 
Sorry, misunderstood what you were trying to do :)

If the second list must come from the SQL and if there is no way to preload all the possible combinations then you must do a round trip to the server to retrieve the data based on the &quot;on Change&quot; Selection (from option 1). In this case I would do it all with server side script and refresh the page (onChange=document.form['myForm'].submit()) with the new values.

A more sophisticated (sp?) method would be to return all the option-2 lists for each of the option-1 selections so all the necessary info exists on the client in an XML DOM. Using the Microsoft DOM XML object use XSLT and the MS XML DOM to dynamically transform the data on the client on the fly. Though I think this will only work in IE not in Netscape.

To work with arrays in JS as you are trying to do might work but what you have written won't work because when the GetTopics() function is called the server side script (in the <% %>) does not exist on the client (and can't by definition). You have to pass the server side value (in the <%%> to the client side script (in the <SCRIPT></SCRIPT>).

To return values from the server in the JS function try this:

<SCRIPT LANGUAGE=&quot;JavaScript&quot;>
function GetTopics(SelectNet){
<% 'open the data connection %>
<!--#include file=&quot;./opendatabase.asp&quot;-->
<% dim arrTopics
'SQL query to the database...
SQL = &quot;SELECT Topic FROM Topics&quot; %>
<!--#include file=&quot;./opensql.asp&quot;-->
<% if not rs.eof then
arrTopics = rs.getrows()
arrTopicsCount = Ubound(arrTopics)
Else
arrTopicsCount = -1
End IF
%>

var arrTopics = <%=arrTopics%>;


clearList(document.storysubmit.neTopic);
addElement(document.storysubmit.neTopic, arrTopics, arrTopics);
}
</script>







 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top