Skip To Main Content | Home | Sitemap | Contact
Latest Visitors: United States's flagUnited States's flagUnited States's flagUnited States's flagUnited States's flagUnited States's flagItaly's flagSweden's flagUnited States's flagSweden's flagUnited States's flagUnited States's flagLatvia's flagParaguay's flagHungary's flagFrance's flagKenya's flagUnited States's flagUnited States's flagUnited States's flagNetherlands's flagChina's flagChina's flagUnited States's flagUnited States's flag
 

How can you create a client side array from a database?

The example uses ASP to connect to a database and return records which are written to a JavaScript array.  For example, you can use the client side array to fill a drop down list, as we do here. In the next example of this, we use the array both to create a select list but also to prefill form fields when a value is selected from the drop down list. Click here for the next example.



<!--METADATA TYPE="typelib" uuid="00000205-0000-0010-8000-00AA006D2EA4" -->
<%
Option Explicit
Session.CodePage=65001
Response.Charset="UTF-8"

Dim cmd, conn, RS, cmdText, param, numAffected

Set cmd = Server.CreateObject("ADODB.Command")
Set conn = Server.CreateObject("ADODB.Connection")
conn.Open Application("examples")

cmdText = "SELECT code, description FROM states"
cmd.commandText = cmdText
Set cmd.ActiveConnection = conn

Set rs = cmd.Execute(numAffected,,adCmdText)
if NOT (rs.bof AND rs.eof) then
  Dim tmpStr
  tmpStr = "<scr" & "ipt type=" & chr(34) & "text/javascr" & "ipt" & chr(34) & ">" & vbLF

  ' declare a client side array
  tmpStr = tmpStr & " var csArray=new Array();" & vbLF & vbLF

  Dim idx, item
  idx = 0 ' JavaSccript arrays are zero based.

  ' fill the array from our recordset
  While NOT rs.eof
    tmpStr = tmpStr & " csArray["& idx &"]=(['"
    for each item in rs.fields
      tmpStr = tmpStr & item & "','"
    next

    ' backoff the trailing ,' and terminate the line with ]);
    tmpStr = Left(tmpStr, Len(tmpStr)-2) & "]);" & vbLF

    ' increment our array index
    idx = idx + 1
    rs.movenext
  Wend

  tmpStr = tmpStr & "</scr" & "ipt>" & vbLF
end if
rs.close
Set rs = nothing
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">

<head>
<title>database to client side array</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="author" content="Roderick Divilbiss">
<meta name="copyright" content="© 2005-2010 Roderick Divilbiss">
<% Response.Write tmpStr %>
<script type="text/javascript">
<!--
function createSelect() {
  var tmpHTML = '<select name="state" onchange="alert(this[selectedIndex].value);">';
  for (var idx=0; idx<csArray.length; idx++) {
    tmpHTML = tmpHTML + ' <option value="' + csArray[idx][0] + '">' + csArray[idx][1] + '</';
    tmpHTML = tmpHTML + 'option>\n';
  }
  tmpHTML = tmpHTML + '</';
  tmpHTML = tmpHTML + 'select>';
  return tmpHTML;
}
//-->
</script>

</head>

<body>
<form action="ASP2ClientSideArray.asp" method="post">
<p><script type="text/javascript">document.write(createSelect());</script>
</form>
</body>
</html>


This Weeks Most Popular Pages Newest Pages