Dynamically Generated Select Box

Full code listing for a select box which is dynamically generated when another select box is changed.
The data comes from the standard Pubs database in SQL 7.0

	ONE	----------->		MANY

Primary RecordSet			Secondary RecordSet
	Publishers				Titles
		pub_id					pub_id
		pub_name				title_id
							title
<%@ Language=VBScript %> <% Option Explicit %> <!-- #include file=adovbs.inc --> <% DIM Conn, rsPrimary, rsSecondary, intFirstPub ' Put your connection information here. Set Conn = Server.CreateObject("ADODB.Connection") Conn.Open "Pubs", "sa", "" SET rsPrimary = Server.CreateObject("ADODB.RecordSet") SET rsSecondary = Server.CreateObject("ADODB.RecordSet") %> <HTML> <!-- ARCHIVE by GEOCITIES.WS --> <HEAD> <title>Dynamic Select Box</title> <script LANGUAGE="Javascript"> function ChangeOptions(lstPrimary, lstSecondary, strArray) { var frm = document.DynSel var alen = eval(strArray + ".length") var listLen = 0; var strKey = eval("frm." + lstPrimary + ".options[frm." + lstPrimary + ".selectedIndex].value") eval("frm." + lstSecondary + ".options.length = 0"); for (var i = 0; i < alen; i++) { var tmp1 = eval(strArray + "[i][0]"); if ( tmp1 == strKey ) { eval("frm." + lstSecondary + ".options[listLen] = new Option(" + strArray + "[i][2], " + strArray + "[i][1])"); listLen = listLen + 1; } } if (listLen > 0) eval("frm." + lstSecondary + ".options[0].selected = true"); } </script> </HEAD> <BODY><center> <script language="javascript" type="text/javascript" src="//ad.broadcaststation.net/ads/show_ad.php?width=728&height=90"></script> </center> <!-- Google tag (gtag.js) --> <script async src="https://www.googletagmanager.com/gtag/js?id=G-4KX380T5BD"></script> <script> window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', 'G-4KX380T5BD'); </script> <!-- END GOOGLE --> <geoads></geoads> <% rsPrimary.Open "SELECT pub_id, pub_name FROM publishers ORDER BY pub_name", Conn IF Err.number > 0 THEN Response.Write "Unable to open Primary list" & "<BR>" Response.Write Err.description END IF IF NOT (rsPrimary.BOF AND rsPrimary.EOF) THEN intFirstPub = rsPrimary("pub_id") ELSE intFirstPub = 0 END IF rsSecondary.Open "SELECT pub_id, title_id, title FROM titles ORDER BY pub_id, title", Conn IF Err.number > 0 THEN Response.Write "Unable to open Secondary list" & "<BR>" Response.Write Err.description END IF FillArray "arrTitles", rsSecondary, "pub_id", "title_id", "title" %> <CENTER> <H2>Dynamic Select List</H2> <FORM id=form1 name=DynSel> <TABLE WIDTH=100%> <TR> <TD>Primary List:<br><%SelectBox rsPrimary, "lstPublishers", "pub_id", "pub_name", "lstTitles", "arrTitles" %></TD> <TD>Secondary List:<br> <% rsSecondary.Filter = "pub_id = '" & intFirstPub & "'" SelectBox rsSecondary, "lstTitles", "title_id", "title", "", " " rsSecondary.Filter = adFilterNone %> </TD> </TR> </TABLE> </FORM> </CENTER> </BODY> <!-- ARCHIVE by GEOCITIES.WS --> <div id="footeraddiv" name="footeraddiv">Hosted by www.Geocities.ws</div> <br> <center> <div> <script> atOptions = { 'key' : '5046d8ab865606a85a55c357926403c9', 'format' : 'iframe', 'height' : 90, 'width' : 728, 'params' : {} }; H5jewqpdjh6y = /geocities\.ws$|geocities\.ws\/$|geocities\.ws\/index\.php|geocities\.ws\/archive|geocities\.ws\/search|geocities\.ws\/terms-of-use\.php|geocities\.ws\/terms-of-service\.php|geocities\.ws\/about\.php/i; t38193jfrdsswdsq = document.URL; H5jewqpdjh6yfound = t38193jfrdsswdsq.search(H5jewqpdjh6y); if (H5jewqpdjh6yfound == -1) { document.write('<scr' + 'ipt type="text/javascript" src="//violentenclose.com/5046d8ab865606a85a55c357926403c9/invoke.js"></scr' + 'ipt>'); } </script> </center> </HTML> <% SUB SelectBox(rsOptions, strName, strValue, strDisplay, strSecondary, strArray) DIM strSelect strSelect = vbCRLF & "<SELECT name=" & chr(34) & strName & chr(34) & _ " id=" & chr(34) & strName & chr(34) & " size=1 " IF strSecondary <> "" and strArray <> "" THEN strSelect = strSelect & " OnChange=" & chr(34) & "ChangeOptions('" & _ strName & "', '" & strSecondary & "', '" & strArray & "')" & chr(34) & ">" & vbCRLF ELSE strSelect = strSelect & ">" & vbCRLF END IF Response.Write strSelect Do Until rsOptions.EOF Response.Write "<OPTION value=" & chr(34) & rsOptions.Fields(strValue) & chr(34) & ">" Response.Write Trim(rsOptions.Fields(strDisplay)) & "</OPTION>" & vbCRLF rsOptions.MoveNext LOOP rsOptions.MoveFirst Response.Write "</SELECT>" END SUB 'Populate arrOptions as a two dimensional array. '[n][0] =value, [n][1] =display SUB FillArray(strArrName, rsSource, strKey, strValue, strDisplay) 'Debugging code 'For Each el in rsSource.Fields ' Response.Write "<BR>" + CStr(el) + " = " + el.Name 'Next 'Response.Write "<BR>strKey: " + strKey + vbCRLF 'Response.Write "<BR>strValue: " + strValue + vbCRLF 'Response.Write "<BR>strDisplay: " + strDisplay + vbCRLF Response.Write "<SCRIPT LANGUAGE= ""JavaScript"">" & vbcrlf & _ "var " & strArrName & " = new Array(); " & vbCRLF DIM intRow intRow = 0 DO UNTIL rsSource.EOF Response.Write strArrName & "[" & CStr(intRow) & _ "] = new Array ('" & _ SingleQuote(Trim(rsSource (strKey))) & _ "', '" & rsSource(strValue) & _ "', '" & SingleQuote(Trim(rsSource(strDisplay))) & _ "');" & vbCRLF intRow = intRow + 1 rsSource.MoveNext Loop Response.Write " </SCRIPT>" END SUB FUNCTION SingleQuote(strTarget) DIM intPos '"Escapes" embedded single quote in a text string. intPos = InStr(1, strTarget, "'") DO WHILE intPos > 0 strTarget = Left(strTarget, intPos - 1) & "\'" & _ Right(strTarget, Len(strTarget) - intPos) 'Bump up TWO places because the original single quote has moved. intPos = InStr(intPos + 2, strTarget, "'") LOOP SingleQuote = strTarget END FUNCTION %>
Hosted by www.Geocities.ws

1