Note the use of the parameterized SQL library to reduce the number of lines of code to access to the database, encapsulates the data access, and perform extensive error handling.
<!--METADATA TYPE="typelib" uuid="00000205-0000-0010-8000-00AA006D2EA4" --> <% Option Explicit Session.CodePage=65001 Response.Charset="UTF-8" %> <!--#include virtual="include/paramSQL.asp"--> <%
Dim cmdTxt cmdTxt = "SELECT company, address, address1, city, state, postalCode, phoneNumber FROM companys ORDER BY company;" openCommand Application("examples"),"getCompanies 1" getRSdynamic db_rs, cmdTxt, "getCompanies 2" if NOT (rs.bof AND rs.eof) then '**************************************************** ' We have to break the <script> tag to avoid an error ' begin a client side script block '**************************************************** 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. tmpStr = tmpStr & " var csArrayMaxIdx=" & rs.recordcount-1 & ";" & vbLF & vbLF '**************************************************** ' fill the array from our recordset '**************************************************** While NOT rs.eof tmpStr = tmpStr & " csArray["& idx &"]=['" for each item in rs.fields tmpStr = tmpStr & item & "','" next '**************************************************** ' back off 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 closeCommand closeRS %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html lang="en"> <head> <title>ASP Database To JavaScript 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 language="javascript" type="text/javascript">
<!--
// currentIdx will hold the array index currently selected so
// we can tell if the form fields are already filled with a
// value from our database array. This allows us to confirm
// any changes if a new value is selected.
var currentIdx='';
function createSelect() {
var tmpHTML = '<select id="companySelect" name="companySelect"'); tmpHTML += ' onchange="fillFormFields(this.selectedIndex);">';
for (var idx=0; idx<=csArrayMaxIdx; idx++) {
tmpHTML = tmpHTML + ' <option>' + csArray[idx][0] + '</option>\n';
}
tmpHTML = tmpHTML + '</select>';
return tmpHTML;
}
function fillFormFields(idx) {
if (currentIdx!='') {
var cnfrm = confirm('Do you want to replace the form information\nfor ' +document.getElementById('company').value+' with\nthe information for ' +csArray[idx][0]+'?');
}
if ((currentIdx=='')||(cnfrm)) {
document.getElementById('company').value=csArray[idx][0];
document.getElementById('address').value=csArray[idx][1];
document.getElementById('address1').value=csArray[idx][2];
document.getElementById('city').value=csArray[idx][3];
document.getElementById('state').value=csArray[idx][4];
document.getElementById('postalCode').value=csArray[idx][5];
document.getElementById('phoneNumber').value=csArray[idx][6];
currentIdx=idx;
}
}
//-->
</script>
<style>
<!-- body { font-family:Verdana, Arial, Helvetica, Sans Serif; font-size: x-small; } #container { width:600px; margin-left:auto; margin-right:auto; } #selCompany { width:250px; float:left; margin:0px 10px 10px 0px; } #compDetails { width:340px; float:right; } h4 { color: #000000; border: 1px solid #000000; background-color: #FF9900; text-align:center; font-size:small; font-weight:bold; margin: 0px 0px 2px 0px; } input, select { font-size:x-small; color:#333333; border:1px solid #000099; margin-bottom:8px; } fieldset { padding: 5px; border:1px solid #000000; } legend { display:none; } label { color:#000099; font-weight:bold; float:left; } #lCompanySelect { width:70px; } #companySelect { width:150px; } #lCompany { width:70px; } #company { width:230px; } #lAddress { width:70px; } #address { width:230px; } #lAddress1 { width:70px; } #address1 { width:230px; } #lCity { width:70px; } #city { width:120px; } #lState { width:40px; } #state { width:20px; } #lPostalCode { width:10px; } #postalCode { width:60px; } #lPhoneNumber { width:70px; } #phoneNumber { width:100px; } .breaker { clear:both; padding:1px; }
-->
</style>
</head>
<body>
<div id="container"> <form method="post"> <div id="selCompany"> <h4>Select Company</h4> <fieldset> <legend>Select Company</legend> <label for="companySelect" id="lCompanySelect">Company Name</label> <script language="javascript" type="text/javascript"> document.write(createSelect()); </script> </fieldset> </div> <div id="compDetails"> <h4>Company Details</h4> <fieldset> <legend>Company Details</legend> <label for="company" id="lCompany">Company</label> <input id="company" name="company" type="text" title="Enter the company name or select a company from the company name list." size="35"> <div class="breaker"></div> <label for="address" id="lAddress">Address (line one)</label> <input id="address" name="address" type="text" title="Enter the company's first address line or select from the company list." size="35"> <div class="breaker"></div> <label for="address1" id="lAddress1">Address (line two)</label> <input id="address1" name="address1" type="text" title="Enter the company's second address line or select from the company list." size="35"> <div class="breaker"></div> <label for="city" id="lCity">City, St., Zip</label> <input id="city" name="city" type="text" title="Enter the company's city or select a company from the company name list." size="15">, <input id="state" name="state" type="text" title="Enter the company's state or select a company from the company name list." size="2">., <input id="postalCode" name="postalCode" type="text" title="Enter the company's Zip Code or select from the company list." size="10"> <div class="breaker"></div> <label for="phoneNumber" id="lPhoneNumber">Phone Number</label> <input id="phoneNumber" name="phoneNumber" type="text" title="Enter the company's telephone number or select from the company list." size="15"> </fieldset> </div> </form> </div> </body> </html>
Date Validation Using JavaScript .
Cross-Browser Clipboard Copy .
Loading Images With Remote Scripting .
Why JavaScript In Hyperlinks Is Bad .
Change The Submit Button To Show Waiting For AJAX Response .
European Date Validation Using JavaScript .
Database Results To Client Side Array .
Reading Files With JavaScript .
AJAX For Plain Text And HTML .