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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to access variable from subroutine or function in <body> ??

Status
Not open for further replies.

snr

Programmer
Oct 8, 2001
78
US
Hi,
I have defined a list box "A" in one form. Depending on the value of the list box item "A", I have to create another list box "B".
How can get the value of A, to create a recordset for B?
I have tried by writing onchange trigger on A ... But it doesnt allow me to create recordset in the function/Subroutine. Even I am not able to access the variable defined in the function / Subroutine , in the main program.

Thanks.

 
Its easier to see what is happening if you post the code
 
<body>

<script language = &quot;vbscript&quot;>
sub get_client()
manid = document.xyz.manid.value
end sub
</script>

<form method=&quot;POST&quot; action=&quot;abc.asp&quot; name = &quot;xyz&quot; id= &quot;xyz&quot;>
<select tabindex=&quot;1&quot; id=&quot;ManID&quot; name=&quot;ManID&quot; onfocus= &quot;get_client()&quot;>
<option VALUE=&quot;1&quot;>1</option>
<option VALUE=&quot;2&quot;>2</option>
<option VALUE=&quot;3&quot;>3</option>
<option VALUE=&quot;4&quot;>4</option>
</select>

<%
set cnn = Server.CreateObject(&quot;ADODB.connection&quot;)
set rs = server.CreateObject(&quot;ADODB.recordset&quot;)
strcnn= &quot;PROVIDER=SqlOLEDB.1;.......................&quot;
cnn.open strcnn
dim sqlstmt
sqlstmt =&quot;select client from clientmaster where man_id ='&quot; & manid & &quot;'&quot;

*************************************
' The manid , is the variable value of that list item, which I am trying to get from get_client() .
*************************************************
rs.open sqlstmt,cnn,adopendynamic,adlockoptimistic,adcmdtext
%>
******************************************
I have to create one more list box from &quot;rs&quot;
*******************************************


</form>
 
It looks like you are mixing client side with server side vbscript. To do this server side:

<body>

<script language = &quot;vbscript&quot;>
sub secondDropDownOnChange()
document.xyz.action=&quot;abc.asp&quot;
document.xyz.submit()
end sub
</script>

<form method=&quot;POST&quot; action=&quot;<%=Request.ServerVariables(&quot;SCRIPT_NAME&quot;)%>&quot; name = &quot;xyz&quot; id= &quot;xyz&quot;>
<select tabindex=&quot;1&quot; id=&quot;ManID&quot; name=&quot;ManID&quot; onchange= &quot;document.xyz.submit()&quot;>
<option VALUE=&quot;1&quot;>1</option>
<option VALUE=&quot;2&quot;>2</option>
<option VALUE=&quot;3&quot;>3</option>
<option VALUE=&quot;4&quot;>4</option>
</select>

<%
manid=Request.Form(&quot;ManID&quot;)
Response.Write manid
'check the variable manid. if is is
' not blank then display the other
'drop down
if manid<>&quot;&quot; then
set cnn = Server.CreateObject(&quot;ADODB.connection&quot;)
set rs = server.CreateObject(&quot;ADODB.recordset&quot;)
strcnn= &quot;PROVIDER=SqlOLEDB.1;.......................&quot;
cnn.open strcnn
dim sqlstmt
sqlstmt =&quot;select client from clientmaster where man_id ='&quot; & manid & &quot;'&quot;

*************************************
' The manid , is the variable value of that list item, which I am trying to get from get_client() .
*************************************************
rs.open sqlstmt,cnn,adopendynamic,adlockoptimistic,adcmdtext

end if
%>
******************************************
I have to create one more list box from &quot;rs&quot;
*******************************************


</form>
 
I didn't get what you mean.......
Can you please explain it ?

Thanks
 
OK

<body>
*********************
this is a client side script. the server does not know what is happening here.You have a variable called manid.
**************************************
<script language = &quot;vbscript&quot;>
sub get_client()
manid = document.xyz.manid.value
end sub
</script>

<form method=&quot;POST&quot; action=&quot;abc.asp&quot; name = &quot;xyz&quot; id= &quot;xyz&quot;>
<select tabindex=&quot;1&quot; id=&quot;ManID&quot; name=&quot;ManID&quot; onfocus= &quot;get_client()&quot;>
<option VALUE=&quot;1&quot;>1</option>
<option VALUE=&quot;2&quot;>2</option>
<option VALUE=&quot;3&quot;>3</option>
<option VALUE=&quot;4&quot;>4</option>
</select>

This starts your server side script. You are trying to access the variable manid that is not declared on the server. the only place that I see it declared is in the client side script above. To make this available to the server you need to resubmit the page after the value in the list box changes then capture that value using the request object like this
<%manid=request.form(&quot;ManId&quot;)
%>
Then you can use the value to create a server side recordset hitting the database with the value from the first select box
<%
set cnn = Server.CreateObject(&quot;ADODB.connection&quot;)
set rs = server.CreateObject(&quot;ADODB.recordset&quot;)
strcnn= &quot;PROVIDER=SqlOLEDB.1;.......................&quot;
cnn.open strcnn
dim sqlstmt
sqlstmt =&quot;select client from clientmaster where man_id ='&quot; & manid & &quot;'&quot;

*************************************
' The manid , is the variable value of that list item, which I am trying to get from get_client() .
*************************************************
rs.open sqlstmt,cnn,adopendynamic,adlockoptimistic,adcmdtext
%>
******************************************
I have to create one more list box from &quot;rs&quot;
*******************************************


</form>
 
Thanks .............
I will try this & definitely let you know.

Thanks again !
 
It is giving problem on
sub secondDropDownOnChange()
this line saying &quot;invalid character&quot;

I don't think there is anything wrong . Do I have to make any setting change on server for accepting
Request.ServerVariables(&quot;SCRIPT_NAME&quot;) ?


Did you try running this code on your end ? Is it giving you the same error ?

I am pasting the code once again ........

<%@ Language=VBScript %>
<html>
<head>
<meta name=&quot;GENERATOR&quot; content=&quot;Microsoft FrontPage 5.0&quot;>
<meta name=&quot;ProgId&quot; content=&quot;FrontPage.Editor.Document&quot;>
<meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=windows-1252&quot;>


<script language = &quot;vbscript&quot;>
 sub  ClientIDOnChange()
      document.order.submit()
 end sub    
</script>

</head>

<body>

<form method=&quot;POST&quot; action=&quot;<%=request.serverVariables(&quot;SCRIPT_NAME&quot;) %>&quot; name = &quot;order&quot; id= &quot;order&quot;>

<select id=&quot;ManID&quot; name=&quot;ManID&quot; onchange = &quot;document.order.submit()&quot;>
<option VALUE=&quot;ABC&quot;>ABC</option>
<option VALUE=&quot;CDF&quot;>CDF</option>
</select>

<%
manid=Request.Form(&quot;ManID&quot;)
Response.Write manid
if manid <> &quot;&quot; then
 set cnn = Server.CreateObject(&quot;ADODB.connection&quot;)
 set rs = server.CreateObject(&quot;ADODB.recordset&quot;)
 strcnn= &quot;PROVIDER=SqlOLEDB.1;persist securityinfo = false ;initial catalog = XYZ;USER Id=userID;Password=passwd;Data source = SERVER &quot;
cnn.open strcnn
  dim sqlstmt
 sqlstmt =&quot;select cl_id from Tblclmaster where man_id ='&quot; &manid &&quot;'&quot;
 rs.open  sqlstmt,cnn,adopendynamic,adlockoptimistic,adcmdtext %>

<select id=&quot;clientID&quot; name=&quot;ClientID&quot; MAXLENGTH=&quot;20&quot; >
<%do until rs.eof%>
<option value= &quot;<%Response.Write rs(&quot;cl_id&quot;)%>&quot; >
<% Response.Write rs(&quot;cl_id&quot;)
rs.MoveNext
loop
set rs = nothing %>
<option value=&quot;&quot; selected>
</select>
<%end if%>

</form>
</body>
</html>

I don't know whats going wrong..........

Thanks
 
The code works fine on my machine . What is the error you are getting?
 
Oops. Didn't test the second select
change it from
<select id=&quot;clientID&quot; name=&quot;ClientID&quot; MAXLENGTH=&quot;20&quot; >

to this
<select id=&quot;clientID&quot; name=&quot;ClientID&quot; MAXLENGTH=&quot;20&quot; onchange=&quot;ClientIDOnChange()&quot; >



 
Thanks ,
but I had already done that , forgot to write.
1) It is giving error at
sub  ClientIDOnChange()
as &quot; Runtime error ***** Invalid Character &quot;
and it is printing
&quot; name = &quot;order&quot; id= &quot;order&quot;> and then list box.

2) and the second error is
line 21
object doen't support the property Or Method
&quot;document.order&quot;
after I change select 2nd item from list box.

Are you not getting these errors ?
 
What is the name of your page? The request.servervariables(&quot;SCRIPT_NAME&quot;) is a variable with the name of your page.
Try printing that out to see what you get.

You could also change the form tag
<form method=&quot;POST&quot; action=&quot;<%=request.serverVariables(&quot;SCRIPT_NAME&quot;) %>&quot; name = &quot;order&quot; id= &quot;order&quot;>

to
<form method=&quot;POST&quot; action=&quot;Put Your page name here&quot; name = &quot;order&quot; id= &quot;order&quot;>
 
The second error is gone
and even it is not printing
&quot; name = &quot;order&quot; id= &quot;order&quot;> and then list box.


But it is still giving &quot;Invalid Character &quot;
error on the  Sub  ClientIDOnChange()
line ............

 
after the page gets the error goto view source, copy and post that info
 
<html>
<head>
<meta name=&quot;GENERATOR&quot; content=&quot;Microsoft FrontPage 5.0&quot;>
<meta name=&quot;ProgId&quot; content=&quot;FrontPage.Editor.Document&quot;>
<meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=windows-1252&quot;>


<script language=&quot;vbscript&quot;>
 Sub  clientID_onchange()
document.order.action = &quot;abc.asp&quot;
      document.order.submit()
 end sub    
</script>

</head>

<body>

<form method=&quot;POST&quot; action=&quot;order_normal.asp&quot; name = &quot;order&quot; id= &quot;order&quot;>

<select id=&quot;ManID&quot; name=&quot;ManID&quot; onchange =&quot;document.order.submit()&quot;>
<option VALUE=&quot;ABC&quot;>ABC</option>
<option VALUE=&quot;CDF&quot;>CDF</option>
</select>


</form>
</body>
</html>

This is the code , I got from View Source

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top