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

Convert Array from ASP to JS

Status
Not open for further replies.

cumap

IS-IT--Management
Jul 9, 2007
268
US
hello all,
I'm interested in doing a "combination" drop down list so that the 2nd drop down will automatically pull out only datas accordingly to the selection of the 1st box. This part, I found help.

The problem that I need help here is getting asp getRows array (3 fields) and insert into js Array.

I tried
Code:
<%
'dbconnection blah blah
aspArray = rs.getRows
%>
<script language=javascript>
var jsArray = new Array('<%=aspArray%>');
</script>

And right there, I've already get error, nevermind whatever next steps to insert the jsArray into the drop downs as I stated earlier
An unhandled data type was encountered.

I searched around but doesn't seem to find the perfect solution for the particular multidimensional conversion.
 
CORRECTION!!!

Forget about the combination drop down lists, I now only want to have the array conversion works for this particular way
for ie:
<select onselect='getjsArray[x3]'>
<option value='jsArray[x1]'>jsArray[x2]</option>
...
</select>

And, this is how the array is supposed to be aspArray(x1,x2,x3)

Thanks!
 
I don't really understand the revision to your question. However, I can tell you this - javascript will not directly understand how to convert an array from the server into a client-side array. You have to do all of that yourself.

Here's an example:

Code:
<script type="text/javascript">
var a = [];
<%
   'write out a for loop using vbscript to populate the client-side array named "a"
   for each item in asparray
      %>
         a.push("<%=item%>");
      <%
   next
%>
</script>

At this point your client-side code would have an array named "a" that contained all the same information that the server side array contained. Additionally, you could concatenate the server-side array by a unique iterator and then use the split method once the string is pulled into the client-side source, but the above example is probably easier to understand.

-kaht

 
Thank you kaht for the reply, another question for you or anyone might experience this before...

Now that this is my code:
note: a,b,c are the Array's datas
Code:
<select onchange='getid('c')>
for i=0 to ubound(aspArray)
  <option value='a'>b</option>
next
</select>
as you can see, the For loop is located within the <select>, hence, I cannot set the c values dynamically. Though this is more like ASP script but the method and idea are generally similar on how to be able to have the onChange works when the dropdown is selected.
 
I use a variation of kaht's code when moving ASP array data into a client-side JS array:
Code:
<script type="text/javascript">
var a = new Array, ai=0;
<%
   'write out a for loop using vbscript to populate the client-side array named "a"
   for each item in asparray
      Response.Write "a[" & ai & "] = " & Chr(34) & item & Chr(34) & ";" & Chr(13) & Chr(10)
      ai = ai + 1
   next
%>
</script>

Lee
 
Thank trollacious and kaht for the new approach to have ASP script w/i JS, I got to learn that.

On another hand, this is my new approach for the problem at hand:
Well, I'd changed my way a little using a js popup to do the job. In another word, after user make selection for the drop down and hit select button, the onClick function will popup a window carying along is the dropdown selected VALUE, so that in the new window, I can have another asp/sql script to call the 3rd "c" data to work. Not sure the side-effects are going to be in the long run but, for now, I'm happy as a claim.

Thanks again for all of your replies!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top