PDA

View Full Version : How to pass an array from ASP to Javascript?


ocularis
10-28-2002, 08:47 PM
Hi,
Does anyone know if it is possible to pass an array of values from ASP to Javascript on the same page?

I am reading values into an array from a database using the getRows function. I want to be able to manipulate this array client-side using Javascript.

Essentially I am creating an order form that will use Javascript to calculate prices based on quantity and generate a confirmation screen with complete description fields obtained from the database.

If anyone has experience with this, I would appreciate the help.

Roy Sinclair
10-28-2002, 10:38 PM
ASP can output javascript code so all you need to do is iterate through your array in ASP and write out a javascript array declaration.

ocularis
10-28-2002, 11:26 PM
Ok, that makes sense.
So I just make an explicit declaration by writing out the Javascript with ASP?

as a follow-up to that question:
array elements in ASP are accessed like so:
thisArray(colnum,rownum)

in Javascript is it (colnum,rownum) or (rownum,colnum) ?

Thanks

glenngv
10-29-2002, 03:16 AM
it's the same (row,column)


dim arr(5,10)
for i = 0 to ubound(arr,1)
for j = 0 to ubound(arr,2)
'if content is a number
arr(i,j) = 0

'if content is a string
'arr(i,j) = "blah"
next
next
response.write "<script language=""javascript"">" & VbCrLf
response.write "var arr = new Array();" & VbCrLf
for i = 0 to ubound(arr,1)
response.write "arr[" & i & "] = new Array();" & VbCrLf
for j = 0 to ubound(arr,2)
'if content is a number
response.write "arr[" & i & "][" & j & "] = " & arr(i,j) & ";" & VbCrLf

'if content is a string
'response.write "arr[" & i & "][" & j & "] = '" & arr(i,j) & "';" & VbCrLf
next
next
response.write "</script>" & VbCrLf


just take note that in the array arr(5,10) the first dimension contains 6 elements, the 2nd has 11 elements.
so in the javascript code that will be generated, the first dimension ranges from 0-5 and the 2nd is 0-10

ocularis
10-29-2002, 02:26 PM
Thanks for the code. I will give it a try.