?
Solved

Classic ASP javascript button variable and form submission

Posted on 2013-01-03
4
Medium Priority
?
2,805 Views
Last Modified: 2013-01-03
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
0
Comment
Question by:schwientekd
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 33

Accepted Solution

by:
Big Monty earned 2000 total points
ID: 38740946
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:

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

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"
      Else
            MM_editRedirectUrl = "page2.asp"
      End If
0
 
LVL 53

Expert Comment

by:Scott Fell, EE MVE
ID: 38740947
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.
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 38741052
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

0
 

Author Closing Comment

by:schwientekd
ID: 38741461
The hidden form variable and this line was exactly what I was missing.  A+

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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

777 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