Entry
How do I populate ASP Variables in client side Javascript variables without using hidden fields?
Client side Javascript variables using ASP 3.0
Apr 6th, 2008 20:07
ha mo, Eric Moore,
Using SQL Server Northwind Database, here is the code. This presumes
you know a connection to your database, etc. I present just the
ASP SQL code and client-side javascript.
Also, notice I have appended -1 to each string of values returned.
I have found with testing that if 1 value is returned, the array is
not populated. Possibly because there is only one value.
My workaround is to add a -1 at the end of each item returned and
decrement any loops against the information by one.
<script language=Javascript>
<%
' Get Javascript client side variables
' using ASP. ConnectionString defined in global.asa usually.
' Session("ConnectionString") =
' myODBCName is name of system DSN defined in the Administrative
' window Data Sources.
' myDBsaPassword is the sa password of your database
' myDatabaseName is the name of your database.
' This could be adapted using OLE DB as well.
Session("ConnectionString")
= "DSN=myODBCName;UID=sa;PWD=myDBsaPassword;DATABASE=myDatabaseName"
Dim oConn
Set oConn = Server.CreateObject("ADODB.Connection")
oConn.Open = Session("ConnectionString")
Dim count
Dim empId
Dim firstName
Dim lastName
count = 0
SQL = "SELECT EmployeeID, LastName, FirstName FROM Employees"
set rs = oConn.Execute(SQL)
Do Until rs.eof
if count = 0 then
empID = rs("EmployeeID")
firstName = chr(34) & rs("FirstName") & chr(34)
lastName = chr(34) & rs("LastName") & chr(34)
else
empID = rs("EmployeeID") & "," & empID
firstName = chr(34) & rs("FirstName") & chr(34) & "," &
firstName
lastName = chr(34) & rs("LastName") & chr(34) & "," & lastName
end if
count = count + 1
rs.movenext
Loop
empID = empID & ",-1"
firstName = firstName & ",-1"
lastName = lastName & ",-1"
rs.close
set rs = nothing
%>
// now populate the Javascript variables
var empLocalID = new Array(<%=empID%>);
var fNameLocal = new Array(<%=firstName%>);
var lNameLocal = new Array(<%=lastName%>);
// now show them so you know this works!
alert(empLocalID + '\n' + fNameLocal + '\n' + lNameLocal);
</script>
http://www.businessian.com
http://www.computerstan.com
http://www.financestan.com
http://www.healthstan.com
http://www.internetstan.com
http://www.moneyenews.com
http://www.technologystan.com
http://www.zobab.com
http://www.healthinhealth.com