Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing a variable from ASP/VBScript to a JavaScript function. 1

Status
Not open for further replies.

msmith425

MIS
Jan 29, 2002
15
0
0
US
I'm having trouble passing a variable from ASP/VBScript to a JavaScript function.

I'm trying to pass the var_URL to the openwin() function. But keep getting a &quot;Line: 22 Error: 'var_URL' is undefined&quot; in the ASP portion of the code (points to <body> line).

Below is my code: Any steering in the right direction would be appreciated.

<!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0 Transitional//EN&quot;>

<html>
<head>
<title>Test</title>

<script language=&quot;Javascript&quot;>

function openwin(varURL){

var Features;
var URL = '<%=var_URL%>';

//Browser features
Features=&quot;toolbar=no,location=no,directories=no,status=no, menubar=no,scrollbars=no,resizable=yes,width=375,height=200&quot;;

var OpenWindow = window.open(URL,&quot;&quot;,Features);
}
</script>

</head>
<body>
<%
Dim oConn, sConnStr, sTable, qByLastName
sTable = &quot;Users&quot;
sConnStr = &quot;Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Inetpub\ Security Info=False&quot;
set oConn = Server.CreateObject(&quot;ADODB.Connection&quot;)
oConn.Open sConnStr
set oRS = Server.CreateObject(&quot;ADODB.Recordset&quot;)
qByLastName = &quot;SELECT ID, LNAME, FNAME, EXT FROM Users ORDER BY LNAME, FNAME;&quot;
oRS.Open qByLastName, oConn, , , adCmdTable

response.write &quot;<table>&quot;
do while not oRS.EOF

var_URL = &quot;more.asp?dbase=&quot; & sTable & &quot;&id=&quot; & oRS(&quot;ID&quot;)
response.write &quot;<tr><td><a href='#' onclick='openwin(var_URL)'>&quot; & oRS(&quot;LNAME&quot;) & &quot;</a></td></tr>&quot;
oRS.MoveNext
loop
Response.Write &quot;</TABLE>&quot;


oRS.Close
oConn.Close
set oRS = Nothing
%>

</body>
</html>
 
This should work outright (I did not test it).

============================================================

<!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0 Transitional//EN&quot;>
<html>
<head>
<title>Test</title>

<script language=&quot;Javascript&quot;>

function openwin(varID){

var Features;
//
// if you want to query other tables as well
// make it a parameter also!
//
var URL = &quot;more.asp?dbase=Users&id=&quot; + varID;
//Browser features
Features=&quot;toolbar=no,location=no,directories=no,status=no, menubar=no,scrollbars=no,resizable=yes,width=375,height=200&quot;;

var OpenWindow = window.open(URL,&quot;&quot;,Features);
}
</script>

</head>
<body>
<%
Dim oConn, sConnStr, sTable, qByLastName
sTable = &quot;Users&quot;
sConnStr = &quot;Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Inetpub\ Security Info=False&quot;
set oConn = Server.CreateObject(&quot;ADODB.Connection&quot;)
oConn.Open sConnStr
set oRS = Server.CreateObject(&quot;ADODB.Recordset&quot;)
qByLastName = &quot;SELECT ID, LNAME, FNAME, EXT FROM Users ORDER BY LNAME, FNAME;&quot;
oRS.Open qByLastName, oConn, , , adCmdTable

response.write &quot;<table>&quot;
do while not oRS.EOF
' no longer needed
' var_URL = &quot;more.asp?dbase=&quot; & sTable & &quot;&id=&quot; & oRS(&quot;ID&quot;)
response.write &quot;<tr><td><a href='#' onclick='openwin(&quot; & CStr(oRS.Fields(&quot;ID&quot;).Value) & &quot;)'>&quot; & oRS(&quot;LNAME&quot;) & &quot;</a></td></tr>&quot;
oRS.MoveNext
loop
Response.Write &quot;</TABLE>&quot;
oRS.Close
oConn.Close
set oRS = Nothing
%>
</body>
</html>
============================================================
 
gearhead,

I am trying to do somthing on the same lines. I have 2 HTML select inputs, where the contents of the 2nd select depends on the choice of the 1st select input.

Currently the contents of the 2nd select is in a Access database, i was wondering if it were possible to dynamically load the contents of the 2nd select input via the database using ASP.

Eg: have an onChange method for the 1st select input which calls a JavaScript function that updates the 2nd select input according to whats in the database.

Appreciate your help,
Jim :)
 
Gearhead,

Thanks! It works great. I just have a question about the line:

response.write &quot;<tr><td><a href='#' onclick='openwin(&quot; & CStr(oRS.Fields(&quot;ID&quot;).Value) & &quot;)'>&quot; & oRS(&quot;LNAME&quot;) & &quot;</a></td></tr>&quot;

If I want to change the database (sTable), how would I add that to the above line. Can you explain the &quot;oRS.Fields(&quot;ID&quot;).Value&quot; section? How do you differentiate the database values?

Thanks...
 
Sorry for the delay ...

To paratrooper:
You can do this but it requires a requery back to the server and the entire page is reloaded. An alternate is to load two arrays (on page build) which can be queried onChange. This is more advanced and you should review examples, search for &quot;linked select&quot; or &quot;linked lists&quot;.

To msmith425:
If you want to parameterize the entire thing you can, however the sqlString, tablename and columns would need to be in the JS and that would be poor form (security wise).

The form oRS.Fields(&quot;Col&quot;).Value is the formal syntax for what you are currently doing, note that it is the required syntax for the .NET platform as VBScript is replaced by VB.NET.

HTH






 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top