Link to home
Start Free TrialLog in
Avatar of indyng
indyng

asked on

How to insert values into a listbox based on an array?

Hi Experts,

How to insert values into a listbox based on an array?

I have the following ASP code which queries data from a table and reads it into an array. Now I want to use the array to populate a list box in a form. How can does be done?

Thanks


<%
'First declare variables used
 
 
Dim arrLocationID
Dim arrLocationName
Dim arrItemID
Dim arrItemName
 
 
 
arrLocationID = "'"
arrLocationName = "'"
arrItemID = "'"
arrItemName = "'"
 
 
 
'=============================START OF CODE FOR LOCATION DROP DOWN===================
 
 
'Query Location
 
Set rs=Server.CreateObject("ADODB.Recordset")
strSQL = "SELECT LOCATIONS.LocationID, LOCATIONS.LocationName FROM LOCATIONS WHERE (((LOCATIONS.Active)=-1)) ORDER BY LOCATIONS.LocationName;"
rs.Open strSQL,conn
 
 
IF NOT rs.EOF THEN
 
  WHILE NOT rs.EOF
 
    arrLocationName = arrLocationName & rs("LocationName") & "','"
 
    rs.MoveNext
 
  WEND
 
  arrLocationName  = Left(arrLocationName , Len(arrLocationName ) - 2)  ' Get rid of the ,' at the end
 
  rs.Close
  Set rs = nothing
 
END IF
 
 
 
 
 
'=============================END OF CODE FOR LOCATION DROP DOWN===================
%>

Open in new window

Avatar of hielo
hielo
Flag of Wallis and Futuna image

try:
<%
'First declare variables used
 
 
Dim arrLocationID
Dim arrLocationName
Dim arrItemID
Dim arrItemName
 
 
 
arrLocationID = "'"
arrLocationName = "'"
arrItemID = "'"
arrItemName = "'"
 
 
 
'=============================START OF CODE FOR LOCATION DROP DOWN===================
 
 
'Query Location
 
Set rs=Server.CreateObject("ADODB.Recordset")
strSQL = "SELECT LOCATIONS.LocationID, LOCATIONS.LocationName FROM LOCATIONS WHERE (((LOCATIONS.Active)=-1)) ORDER BY LOCATIONS.LocationName;"
rs.Open strSQL,conn
 
 
IF NOT rs.EOF THEN
 Response.Write("<select name='location'>")
  WHILE NOT rs.EOF
   Response.Write("<option value='" & rs("LocationID").value & "'>" & rs("LocationName").value & "</option>")
'    arrLocationName = arrLocationName & rs("LocationName") & "','"
 
    rs.MoveNext
 
  WEND
 Response.Write("</select>")
'  arrLocationName  = Left(arrLocationName , Len(arrLocationName ) - 2)  ' Get rid of the ,' at the end
 
  rs.Close
  Set rs = nothing
 
END IF
 
 
 
 
 
'=============================END OF CODE FOR LOCATION DROP DOWN===================
%>

Open in new window

Avatar of indyng
indyng

ASKER

The data is first queried using ASP and stored in an array.

The array then populates the list box in the form using Java script. This is what I am looking for.

What is the syntax to insert values into a listbox using an array using Java script?

Thanks
>>What is the syntax to insert values into a listbox using an array using Java script?
The array to speak of resides on the server, NOT on the browser. The javascript code would execute on the browser, not on the server. So, javascript will NEVER see that array. You need to use Response.Write("...") to send those values to the browser. Hence, instead of sending a comma-separated list of values, I suggested the ASP code that builds the actual HTML SELECT list.
ASKER CERTIFIED SOLUTION
Avatar of bluV11t
bluV11t
Flag of Norway image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sorry, remove line 39             alert(item);
Debugging script :-)