Pass data from a resultset to Javascript ArrayTag(s): Varia


On the server-side, the ASP code executes a query, the result is written in FORM hidden field (each value is separated by a ","). Then from Javascript (on the client-side), each hidden field value is extracted to an array,

Of course, JSP code can be used instead of ASP!

<%
'---
'ASP (server-side)
'---
Set Connection = Server.CreateObject("ADODB.Connection")
Connection.Open strConn

Set rs = Connection.Execute("SELECT field1, field2 from mytable")
Do While NOT rs.EOF
   field1 = field1 & rs("field1") & ","
   field2 = field2 & rs("field2") & ","
   rs.MoveNext
loop
field1 = Left(field1,len(field1)-1)
field2 = Left(field2,len(field2)-1)
%>

<!--HTML (client-side)-->
<form name="dataholder" action="#">
   <input type="hidden" name="field1" value="<%=field1%>">
   <input type="hidden" name="field2" value="<%=field2%>">
</form>

<script language="JavaScript">
   var something = "";
   //Fetch the data from the server-side
   var field1 = document.dataholder.field1.value;  
   //returns an array
   var arrField1 = field1.split(",");              
   var field2 = document.dataholder.field2.value;   
   var arrField2 = strField2.split(",");        
   for(var i=0; i < field1.length;i++) {
      something += arrField1[i]; 
      something += arrField2[i];
   }
</script>


blog comments powered by Disqus