Link to home
Start Free TrialLog in
Avatar of schwientekd
schwientekdFlag for United States of America

asked on

Classic ASP javascript button variable and form submission

This is a classic asp page.  I've got a button when clicked will run a javascript function to assign a variable and submit the form.

<SCRIPT LANGUAGE="JavaScript">
function myFunction() {
testVar = "N";
document.FormName.submit();
}

When the form is submitted I reference the variable testVar but it is blank.  The form submission code is before the <head> section of the page.  Is what I am trying to do not possible and can anyone suggest a different method?

I want it to get the testVar value and execute this section of code:

Dim MM_editRedirectUrl
      
      If testVar = "N" Then
            MM_editRedirectUrl = "page1.asp"
      Else
            MM_editRedirectUrl = "page2.asp"
      End If
ASKER CERTIFIED SOLUTION
Avatar of Big Monty
Big Monty
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
Are you running the javascript serverside or client side?

If client side, remember that client side loads AFTER all serverside processing and HTML.  Serverside vbscript or javascript runs BEFORE any html and javascript is written to the page.

If you have both serverside javascript and serverside vbscript, I don't believe they can directly talk to each other.   However, they both can accept the same querystring or request.form or request.cookies.
Some of it is at least wrong.  Browsers expect a page to be formatted properly.  Anything before <head> and after </body> in the HTML sent to the browser isn't necessarily recognized.

Also, testVar = "N" in the ASP code and in the javascript are two entirely different variables.  To pass it to ASP code on the form action page, it would have to be a variable in the form.  It can be hidden but the way you are declaring it in that javascript doesn't get it to the next page.

This page http://www.w3schools.com/asp/asp_inputforms.asp is about ASP forms processing and this page http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_form_submit shows form submission with javascript.

This code demonstrates the basics.
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function formSubmit()
{
document.getElementById("testVar").value="N";
document.getElementById("frm1").submit();
}
</script>
</head>
<body>
<h1>ASP Form Test</h1>
<p>Enter some text in the fields below, then press the "Submit form" button to submit the form.</p>

<form id="frm1" action="form_action.asp">
First name: <input type="text" name="fname"><br>
Last name: <input type="text" name="lname"><br><br>
<input type="hidden" name="testVar" id="testVar" value="" />
<input type="button" onclick="formSubmit()" value="Submit form">
</form>
<% 
response.write(request.querystring("fname"))
response.write(" " & request.querystring("lname") & "<br>")
response.write("testVar = " & request.querystring("testVar"))
 %>
</body>
</html>

Open in new window

Avatar of schwientekd

ASKER

The hidden form variable and this line was exactly what I was missing.  A+

document.formname.testVar.value = "N";