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.

function myFunction() {
testVar = "N";

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"
            MM_editRedirectUrl = "page2.asp"
      End If
Who is Participating?
Big MontyConnect With a Mentor Senior Web Developer / CEO of Commented:
you need to create a hidden field on your page so that it gets posted to the next page, creating local variables will not be posted.

in your form, create the hidden field like this:

<input type="hidden" name="testVar" value="" />

and change your javascript function to:

function myFunction() {
document.formname.testVar.value = "N";

now, your if statement should work after you load the testVar value into a local variable on the server:

Dim MM_editRedirectUrl, testVar
testVar = Request.Form("testVar")         '-- this assumes you're using POST as your forms method
      If testVar = "N" Then
            MM_editRedirectUrl = "page1.asp"
            MM_editRedirectUrl = "page2.asp"
      End If
Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
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.
Dave BaldwinFixer of ProblemsCommented:
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 is about ASP forms processing and this page shows form submission with javascript.

This code demonstrates the basics.
<!DOCTYPE html>
<script type="text/javascript">
function formSubmit()
<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">
response.write(" " & request.querystring("lname") & "<br>")
response.write("testVar = " & request.querystring("testVar"))

Open in new window

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

document.formname.testVar.value = "N";
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.