Passing variables between serverside JavaScript & serverside VBScript

Posted on 2006-05-09
Medium Priority
Last Modified: 2010-10-05
I have an ASP page that uses VBScript. There is a form on the ASP page and there is another separate form that when the button is clicked a pop up window appears with a list of employees. When the user picks an employee, that employee number is submitted to the database and the original page is refreshed with the current data. I need to be able to capture any data that was typed into the first form when the second form is submitted. I know this is possible, but I just don't know the magic combination.

I can get the data using clientside JS by using a function similar to the following:

      function SetValue(Formname,Elementname,IDElementname,EmployeeID,Employeename){

But I cannot get this data to my serverside VBScript so that I can display it when the form refreshes.

As far as testing goes, I have tried the following:

<SCRIPT LANGUAGE="JavaScript" RUNAT="Server">      
      function HelloJavaScript() {
            return "Hello World in JavaScript";

And was able to pass the serverside JS values to VBScript:

      Response.Write(HelloJavaScript() + "<br/>")

Which returned the following:

      Hello World in JavaScript

I also tried the following:

      function setDefaultFocus() {
                  dim sMessage
                  sMessage = "This is a Test"

And called it using serverside VBScript:

      Response.Write "sMessage: " & sMessage & "<br>"

Which pops up the alert with the message assigned to it.

I cannot seem to get the JS "return" to work with clientside and I cannot seem to get the JS opener to work serverside.

This all works great, but in order to get the value of the textbox from the form on the first page, I need to be able to use the opener, or I need help figuring out how to do it using VBScript.

Whenever I try to assign the opener to a variable and use it, I get a 'opener' undefined error.

Any help would be greatly appreciated!


Question by:okcuser
LVL 12

Expert Comment

ID: 16642061
It has to be submitted to the server in order for the server to be aware of what the value is.  You would have to add something like this to your setvalue Javascript:


but I am not sure that I completely understand which one is form one and which one is form two in your question.  It might be really helpful to have a simple example posted if the above solution does not help.

Expert Comment

ID: 16642373
Make the button that is clicked a " submit " for the form ... then all the information populated in the first form will be passed in the querystring.  On the pop up page use something like this at the top to populate the DB ...

item1 = Request("item1")
item2 = Request("item2")
SET dbConn = Server.CreateObject("ADODB.Connection")
SQL = "INSERT INTO tblMemo (title,text) VALUES ('" & item1 & "','" & item2 & "')
LVL 28

Expert Comment

ID: 16642469
> I cannot seem to get the JS opener to work serverside.

Of course not, you never will be able to. "opener" is an object on the client. There is no such thing as "opener" on the server.

You should realize that clientside scripts and serverside scripts run at completely different machines, and do not share any variables. They are completely independent, just like the scripts that would run on my machine and on yours. The only thing that connects them is probably this thread, which we both open in our respective browsers.


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

LVL 16

Expert Comment

ID: 16648317
I guess it wasnt explained clearly on your previous question. =(
Make sure your pop up is also a form. Then when you hit submit on the pop up,
 you pass the value from the pop up into the main page. The javascript does that for you:

Make sure your child form looks like this:
<script language="javascript">
function post_value(){
//set parent textbox to equal popup child textbox
opener.document.MainForm.company.value = document.ChildForm.company.value;
//close popup window

<form name="ChildForm" method="post" onSubmit="post_value()">
<select size="1" name="company">
<option value="Company A">Company A</option>
<option value="Company B">Company B</option>
<option value="Company C">Company C</option>
<input type="submit" value="Submit" name="Submit">

So in this case "opener" is your main page. This sets company to the value from the pop up. From there, you're field on the main page should be populated.

Your main page looks like this:

<form name="MainForm" method="post" >
<text name="ID" value="">
<text name="company" value=""> <input type="button" value="Search Company" onclick="window.open('popup.html');">
<input type="submit" value="Submit" name="Submit">
LVL 16

Expert Comment

ID: 16648333
So then in order to do the submission, you need to hit the submit button in the main form to pass all the values thru.
All the javascript does it fill in the textbox. It doesnt do the submitting. That still is done with ASP
LVL 16

Expert Comment

ID: 16648360
Woops main form should look like:

<form name="MainForm" method="post" >
<input type="text" name="ID" value=""><br>
<input type="text" name="company" value=""> <input type="button" value="Search Company" onclick="window.open('testform.html');"><br>

<input type="submit" value="Submit" name="Submit">

I had the textboxes declared wrong.
LVL 16

Expert Comment

ID: 16648405
The only way you can pass a Javascript value to use for ASP is to put it into a form field (ie textbox) or pass it within the URL  (test.asp?company=McDonalds)

As far as I know you cannot pass it directly using Javascript. ASP executes BEFORE Javascript does. So while you can use ASP to _build_ a Javascript function, you can't do it the other way around.

Author Comment

ID: 16651186
Ok...I am essentially using 3 documents (2 ASP pages and one include file). After I describe the three, see if you can tell me if what you mentioned will work:

Doc1 (hz_main.asp) contains a form

<form action="hz_main.asp" method="post" name="frmUpdateHZ">
<input type="hidden" name="Action" value="UpdateHZ">
<textarea class="FormTextArea" name="txtTitle" cols="50" rows="5"><%=varTitle%></textarea>
<textarea class="FormTextArea" name="txtDesc" cols="50" rows="15"><%=varDesc%></textarea>
<select name="cboGetAreaID" class="FormDropDown">Code...</select>
<select name="cboGetCatID" class="FormDropDown">Code...</select>
<select name="cboGetTypeID" class="FormDropDown">Code...</select>
<input type="submit" class="SmFormButtons" value="Update Hazard">

Doc1 (hz_main.asp) also contains a button with a JavaScript action that generates a pop up window, which in turn calls an an include file. The JavaScript action is as follows:

<%Session("RedirectSearchURL")="hz_main.asp?FromSB=Yes&varAction=AddEmpRB&varHZID="& varHZID%>
<a href="javascript:openempsearchredirecthazard('Reported By','frmUpdateHZ')"><img src="../imgs/main/btn_flashlight.gif" width="24" height="24" align="absmiddle" border="0"></a>
&nbsp;Click flashlight for employees...

Doc2 (searchemp_functions.inc) The pop up window uses the function

function openempsearchredirecthazard(vTitle,vFormname){
      var url = "searchpeople.asp?vType=RedirectHazard&vTitle="+vTitle+"&vFormname="+vFormname
      leftPos = 0
      topPos = 0
      if (screen) {
            leftPos = (screen.width / 2) - 225
            topPos = (screen.height / 2) - 150
            var properties = "scrollbars=yes,width=500,height=300,left="+leftPos+",top="+topPos;
            var winpops=window.open(url,"searchpeople",properties)
            window.onunload = function(){winpops.close()}

The function in turn calls another ASP page Doc3 (searchpeople.asp). This ASP page runs code to update the employeeID, without the user seeing it and builds the return URL with the querystring values. The user then returns to Doc1 (hz_main.asp) and falls into my VBScript code based on the Session variable querystring action varAction=AddEmpRB.

My dilemma is this:

My user will fill out the form and they will click the button that activates the pop up window, which updates the employee, but all of the data they typed in the form (Doc1 hz_main.asp) is lost, because they did not physically click the submit button for THAT form. Essentially I need a way to grab the values from the Doc1 form (hz_main.asp) and send them through the pop up window to the ASP page it calls (Doc3 (searchpeople.asp))...and then back to the original ASP page (Doc1 (hz_main.asp)) so that I can update the database and they do not lose their data when they click that button.

Since the include file is in clientside JavaScript, I need a way to convert it, or send it to serverside VBScript.


LVL 16

Accepted Solution

ThinkPaper earned 2000 total points
ID: 16662366
Just to make sure I'm understanding what you're saying...

Form 1
  User fills in data
  User clicks button for pop up
  this calls function to grab all current field values and open new window and attach values in URL
Form 2 (pop up window)
  User selects from listbox company and clicks a button and this activates page 3.
Page 3
  This does an update, but user does not see this page
  Once action is done then return values to pop up window.

Pop up Window passes values back to Form 1 and closes itself.

After all this, user adds other info on Form 1 and finally clicks Submit.

1) Does page 3 contain any NEW info that needs to be passed back to the pop up window? Or is it retaining the info from the pop up window and then passing it back to Form 1?

2) whats the purpose of retaining the URL? is that just info that needs to be in the textbox or is it the URL back to Form 1?

3) If page 3 is just an include, then you can think of it as just an extension of page 2. So you technically have 2 pages, not 3. Which would mean that the javascript I mentioned above should work. Any values being created in Page 3 should automatically be available to you on Page 2 (since it's an include)

4) how are you calling page 3? Are you using an INCLUDE?

It should look something like this on your pop up page:
If request("Submit") <> "" Then
<!--#include file ="page3.asp"-->
'all the updates are complete so submit values back to form 1
End If
<script language="javascript">

The code above basically shows that on the pop up window, it calls Page 3. You don't need to pass any query strings or parameter in the URL because ANY VALUES you provided in page 2 is automatically available to you on Page 3 and vice versa.
The only time you'd need to pass values is jumping back from the pop up window to the Main Form.

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question