Solved

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

Posted on 2008-10-02
5
899 Views
Last Modified: 2012-05-05
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

0
Comment
Question by:indyng
  • 2
  • 2
5 Comments
 
LVL 82

Expert Comment

by:hielo
ID: 22627054
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

0
 
LVL 1

Author Comment

by:indyng
ID: 22627113
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
0
 
LVL 82

Expert Comment

by:hielo
ID: 22628748
>>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.
0
 
LVL 7

Accepted Solution

by:
bluV11t earned 500 total points
ID: 22629171
Don't know why you'd want to do it with JavaScript but here it is....
<%

'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

%>
 

<html>

<script language="JavaScript">
 

function populate(strSelectbox, strArray)

{

var myArray = strArray.split(",");

var mySelect = document.getElementById(strSelectbox);

var myIndex = 0;
 

	for(var item in myArray)

	{

	    var newOption = document.createElement('option');

	    alert(item);

		var myIndex = mySelect.options.length;

		mySelect.options[myIndex] = new Option(myArray[myIndex], myArray[myIndex], false, false); 

		myIndex++;

	}

}

</script>

<body onLoad="JavaScript: populate('location', '<%=arrLocationName%>')">

<select id="location" name="location">
 

</select>

</body>

</html>

Open in new window

0
 
LVL 7

Expert Comment

by:bluV11t
ID: 22629274
Sorry, remove line 39             alert(item);
Debugging script :-)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Sums of coloumns in html/java 15 67
Change javascript css 1 20
using php variable inside javascript 5 15
insert text field data into html script 13 26
In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now