Link to home
Start Free TrialLog in
Avatar of okcuser
okcuser

asked on

Passing variables between serverside JavaScript & serverside VBScript

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){
            opener.document.forms[Formname].elements[IDElementname].value=EmployeeID
            opener.document.forms[Formname].elements[Elementname].value=Employeename
            self.close()
      }

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";
       }
</SCRIPT>

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() {
            document.frmSearch.txtEmpSearch.focus();
            <%
                  dim sMessage
                  sMessage = "This is a Test"
            %>
            
            alert('<%=sMessage%>');
      }

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!

Thanks,

Tammi
Avatar of peterxlane
peterxlane

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:

opener.document.forms[Formname].submit();


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.
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")
'YOUR DB CONNECTION INFO
SQL = "INSERT INTO tblMemo (title,text) VALUES ('" & item1 & "','" & item2 & "')
dbConn.Execute(SQL)
%>
Avatar of sybe
> 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.

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:
<head>
<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
self.close();
}
</script>
</head>
<body>

<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>
</select>
<input type="submit" value="Submit" name="Submit">
</form>
</body>

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">
</form>
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
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">
</form>

I had the textboxes declared wrong.
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.
Avatar of okcuser

ASKER

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">
</form>

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.

Thanks,

Tammi
ASKER CERTIFIED SOLUTION
Avatar of ThinkPaper
ThinkPaper
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial