Home|Sitemap|Contact

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, 2006 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>


Most Popular Pages On rodsdot.com

Client Side Includes Using JavaScript
Creating A JavaScript Slider Input in JavaScript
Creating Conditional Links With ASP
Dynamically Adding Rows and Fields To A Table Based Form
Geo-Coding IP Addresses
How To Make A Pop Over Menu
JavaScript Date Validation DD/MM/YYYY
JavaScript Date Validation MM/DD/YYYY
Parameterized SQL Using Multiple Form Inputs and Filtering
Play A WAV File On Mouseover
Pop Over Form
Resorting A Multi-Dimensional Table Using Client-Side JavaScript
Scripting Remote Images in JavaScript
Using AJAX to Return HTML For Dynamic Forms
Using Images For CAPTCHA
Using Parameterized SQL with ASP and MS Access
Why JavaScript As The HREF Of A Link Is Bad