When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles
Related Web Technologies
User Tips!
Coding Tips

Sample Chapters
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Stump the SQL Guru!
XML Info
Author an Article

The 4 Guys Present: ASPFAQs.com

Jump to a FAQ
Enter FAQ #:
..or see our 10 Most Viewed FAQs.

4GuysFromRolla.com : ASP FAQS : The Nature of Things


How do I send variables and data from client-side JavaScript to server-side ASP?

[Print this FAQ]

Answer: Variable values that exist in client script can be sent to ASP on the server-side in two ways.

First, you can write the values you want to a form field, hidden or visible, and submit the form to the ASP page you wish to receive the variables on. It's important to understand that only data that can be expressed as text can be sent by this method. If what you want to send is complex, like a JavaScirpt array or object, you will have to boil it down to text and numbers and re-build it on the server-side. A simple example of this method is below:

        <script language="JavaScript" type="text/javascript">
        function SubmitThisValue(varValue) {
        // Set the value of the first element of the first form on the page:
            document.forms[0].elements[0].value = varValue;

        // Submit that form (Note: if you submit to the same page
        //    this will loop, forever submitting your form):
        <form name="txt" action="SubmitPage.asp">
            <!--- below is the form field that gets the value -->
            <input type="textbox" name="values1">
        <script>SubmitThisValue("Texas");    </script>

A server-side ASP script can then recieve the values and interact with them as necessary through the Request.Form and Request.Querystring collections, depending on whether you use the POST or GET method (attribute on the <form> tag: method="POST" or method="GET"), respectively.

Second, you can use Microsoft's Remote Scripting - which is essentially a Java applet that runs invisibly in the browser and allows calls to be made to ASP pages as if they were objects. The ASP pages must be set up specifically to use remote scripting, however it has several advantages over submitting data with a form:

1) There is no change in page state, so everything else on the page remains untouched (therefore you don't have to manage the state of any other form elements on the page).

2) Calls can be made asynchronously, that is, you don't have to wait for the call to finish to do something else, the call is made and returns control to your client-side script. When the call is finished, there is a mechanism that will run code (like a VB/VBA event) for you.

3) Although complex data structures can't usually be sent directly between a web client and ASP page, remote scripting makes this work quite well. You can pass script objects back and forth between the client and server, for example an array in JScript created in the browser can be passed to the server ASP page and used there.

Remote scripting is certainly non-trivial, and not terribly intuitive to work with, however it is effective in the right situation and works even in Netscape browsers up to the last 4.x version (and not with the new version 6).
For more information see Microsoft's Scripting website and read the documentation and examples carefully. First try modifying one of the examples to work for you before you try to write one from scratch.

FAQ posted by Richard Lowe at 1/14/2001 3:10:58 AM to the The Nature of Things category. This FAQ has been viewed 57,524 times.

Do you have a FAQ you'd like to suggest? Suggestions? Comments? If so, send it in! Also, if you'd like to be a FAQ Admin (creating/editing FAQs), let me know! If you are looking for other FAQs, be sure to check out the 4Guys FAQ and Commonly Asked Messageboard Questions!

Most Viewed FAQs:

1.) How can I format numbers and date/times using ASP.NET? For example, I want to format a number as a currency. (761643 views)
2.) I am using Access and getting a 80004005 error (or a [Microsoft][ODBC Microsoft Access Driver] The Microsoft Jet database engine cannot open the file '(unknown)' error) when trying to open a connection! How can I fix this problem? (207777 views)
3.) How can I convert a Recordset into an array? Also, how can I convert an array into a Recordset? (202549 views)
4.) How can I quickly sort a VBScript array? (196039 views)
5.) How can I find out if a record already exists in a database? If it doesn't, I want to add it. (156019 views)
6.) How do I display data on a web page using arrays instead of Do...While...MoveNext...???... (152331 views)
7.) When I get a list of all files in a directory via the FileSystemObject, they aren't ordered in any reasonable way. How can I sort the files by name? Or by size? Or by date created? Or... (140381 views)
8.) For session variables to work, must the Web visitor have cookies enabled? (110162 views)
9.) Can I send emails without using CDONTS? (107083 views)
10.) How can I take the result of a SELECT...MULTIPLE or a group of same-named checkboxes and turn it into a query? That is, if the user selects 3 answers, how can I construct a query that looks for all 3? (106308 views)
Last computed at 9/17/2007 3:22:00 AM

ASP.NET [1.x] [2.0] | ASPFAQs.com | Advertise | Feedback | Author an Article