Pass variables from Javascript to ASP code?

SONYFX

Senior member
May 14, 2003
403
0
0

I was wondering if anyone out there knows how i can pass a variable from my JavaCsript function to my ASP function.

I have a list from which I call a javascript function with onChange(javascript function). Inside that function, i want to pass a variable to an ASP function which will use it in a SQL query.

I have tried everything i can imagine but to no avail.

Thanks in advance for any suggestions that can be offered.
 

bersl2

Golden Member
Aug 2, 2004
1,617
0
0
ASP is server-side. Javascript is client-side. You have to pass the results as parameters to another page.
 

MrChad

Lifer
Aug 22, 2001
13,507
3
81
There are three ways of going about this.

The first two are the most common, the third is more complex but also more flexible.

1. Redirect to a URL (HTTP GET) and pass the variable in the URL's query string.

e.g.

function myFunction(){
....var myVar = "test";
....var sURL = "process.asp?var=" + myVar;

....window.location.href = sURL;
}

In your process.asp, you can read Request.QueryString(var) to retrieve the value.

2. Post the value in a form (HTTP POST)

e.g.

function myFunction(){
....var myVar = "test";

....document.myForm.var.value = myVar;
....document.myForm.action.value = "process.asp";
....document.myForm.submit();
}

In process.asp, you can read Request.Form(var) to retrieve the value.

3. You can use AJAX to submit the value in an asynchronous HTTP call to process.asp. Process.asp can return some value that you can process within JavaScript. This is a bit more complicated, but obviously more flexible (the user is not redirected away from the starting page). Google AJAX for more information.