Now we can do this all with one array and one function. In this example I'll show you how to create the array dynamically on the server, but first we need to see what it should look like statically. All will feed off a form that will look something like this:
<select name="types" onChange="fillItems(0)">
The array we will need will be multi-dimensional. In the first dimension, each item will contain an array with three indexes. The first will be the ID of the main category that it belongs to; in the example it will be either laptops (ID: 32) or PDAs (ID: 27). The second index will contain the actual unique ID of the item. The third will just be the description. In the end it should look like this:
arItems = [
[ 32, 234, 'Toshiba SuperLaptop'],
[ 32, 156, 'Compaq WonderMachine'],
[ 32, 333, 'Sony ThinBook'],
[ 27, 656, 'IBM c6'],
[ 27, 467, 'Palm HandThing']
So that's the end product, now I'll show you how to generate it. I'm assuming that you'll retrieve a result set from a database with ASP.
var arItems = new Array()
arItems = [
strSQL = "SELECT type_id, id, description FROM items"
objRS.Open strSQL, strConn
arItems = objRS.GetRows()
Now we dump the contents of the server-side array into the identically-named client-side array.
for i = 0 to uBound( atItems, 2 )
response.Write( "[" & atItems( 0, i ) & "," & atItems( 1, i ) & _
",'" & atItems( 2, i ) & "']" )
if i < uBound( atItems, 2 ) then response.Write("," & vbCrLf )