Solved

Classic ASP javascript button variable and form submission

Posted on 2013-01-03
4
2,464 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
4 Comments
 
LVL 32

Accepted Solution

by:
Big Monty earned 500 total points
Comment Utility
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 52

Expert Comment

by:Scott Fell, EE MVE
Comment Utility
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 82

Expert Comment

by:Dave Baldwin
Comment Utility
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
Comment Utility
The hidden form variable and this line was exactly what I was missing.  A+

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

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

762 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now