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

How can I access JavaScript variable from asp?

Status
Not open for further replies.

vicky2003

Programmer
May 27, 2003
22
CA
HI friends,
I got a problem here , anyone could help me?
I'm creating a web form,user can choose different table in this form, then the form will show all the fields in this table. SO I add a event: OnTableSelectChange(). In this function, I try to get table's name,then use ADO get field name from database,the code is :
{
strTableName = document.frmEditSql.elements["selTableName"];
<%
Dim rs
Set cnnADO = dbGetConnection()
strSQL= &quot;select * from &quot; & strTableName
strSQL = strSQL &&quot; where ROWNUM<1&quot;
Set rs = cnnADO.Execute(strSQL)
count = rs.Fields.Count
dim index
For index=0 to count-1
%>objFields.options[<%= index %>] = new Option(&quot;<%= rs.fields(index).name%>&quot;,&quot;<%= rs.fields(index).name%>&quot;);

<%
Next
%>

}
But in asp code, it can't get strTableName from JavaScript. How can I make it work?
Thanks!
vicky
 
you need to submit the value to the server in some way

thread216-557022

_________________________________________________________
[sub]$str = &quot;sleep is good for you. sleep gives you the energy you need to function&quot;;
$Nstr = ereg_replace(&quot;sleep&quot;,&quot;coffee&quot;,$str); echo $Nstr;[/sub]
onpnt2.gif
[sup] [/sub]
 
do you mean &quot;submit&quot; this page to server?
If it is,whole pages contents will be update,I can't let it happen. have any idea?

vicky
 
Hi vicky,
yes, you need to determine a way to get the value that you want to be incorpurated into your client level scripting to the server side scripting (ASP) there is no dynamic interaction between the two. The most common ways of performing this task is using hidden form fields to send the values or URL querystrings. eg: page.htm?variable=test



_________________________________________________________
[sub]$str = &quot;sleep is good for you. sleep gives you the energy you need to function&quot;;
$Nstr = ereg_replace(&quot;sleep&quot;,&quot;coffee&quot;,$str); echo $Nstr;[/sub]
onpnt2.gif
[sup] [/sub]
 
if do so,how can I make sure every items in the form keep previous value? Should I make it myself or use some character of IE(I don't know) when the form loading?
really appreciate your help
 
the request object should be all you need.
eg:
when you load the form just include the value attrib to call for the request value
<input type=&quot;text&quot; name=&quot;txt&quot; value=&quot;<%=Request.Form(&quot;txt&quot;)%>&quot;>

what is going to happen here is if the request value is emtpy the value taht is loaded into the input will simply be empty. but if the page had been submitted due to you needing a server script run then when the page loads again the value that the user entered thus will be there again.

there are some simple validation steps you can also take to figure on what you want to load in the page or not.
for example the entire page can become a If conditional statement
<%
If Request.Form(&quot;txt&quot;) <> &quot;&quot; Then
load something
Else
%>
<body>
<form>
<input type=&quot;txt&quot; name=&quot;txt&quot;>
</form>
<% End If %>

etc...

This being a page that submit's to itself in the action of the form as a page named page.asp having the form tag
<form action=&quot;page.asp&quot; method=&quot;post&quot;>

hope that helps out a bit
if you still have problems can you give details as to what the page is doing and what you need it to do.

_________________________________________________________
[sub]$str = &quot;sleep is good for you. sleep gives you the energy you need to function&quot;;
$Nstr = ereg_replace(&quot;sleep&quot;,&quot;coffee&quot;,$str); echo $Nstr;[/sub]
onpnt2.gif
[sup] [/sub]
 
Sometimes it's easier just to include a hidden iframe on your page. Submit your request for the little information you need from the db through this iframe so the main frame stays as it is. Then, put an onload attribute in the body tag of the page that loads in the iframe that will put the retrieved data on the main page. For example, the following code will take the zip code the user enters and fill in the City and State for that zip code without leaving the main page.

<!-- main page -->
<html>
<head>
<script language=&quot;JavaScript&quot;>
function getCityState(zip){
document.ihidden.location.href='getCityState.asp?zip='+zip;
}
</script>
</head>
<body>
<iframe name=&quot;ihidden&quot; style=&quot;position:absolute;visibility:hidden&quot;></iframe>
<form name=&quot;mainForm&quot; action=&quot;mainPageHandler.asp&quot; method=&quot;post&quot;>
<input type=&quot;text&quot; name=&quot;City&quot; readonly><br>
<input type=&quot;text&quot; name=&quot;State&quot; readonly><br>
<input type=&quot;text&quot; name=&quot;ZipCode&quot; onblur=&quot;getCityState(this.value)&quot;>
</form>
</body>
</html>

<!-- checkZip.asp -->
<html>
<head>
<script language=&quot;JavaScript&quot;>
function doload(){
<%
'Put code to open your db here

Set rsCityState = conn.Execute(&quot;SELECT * FROM Zips WHERE ZipCode='&quot; & Request(&quot;zip&quot;) & &quot;'&quot;)
If rsCityState.EOF Then
Response.Write &quot;alert('Zip Code not found!')&quot;
Else
Response.Write &quot;parent.document.mainForm.City.value='&quot; & rsCityState(&quot;City&quot;) & &quot;';&quot;
Response.Write &quot;parent.document.mainForm.State.value='&quot; & rsCityState(&quot;State&quot;) & &quot;';&quot;
End If
%>
}
</script>
<body onload=&quot;doload()&quot;>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top