• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2988
  • Last Modified:

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
0
schwientekd
Asked:
schwientekd
1 Solution
 
Big MontySenior Web Developer / CEO of ExchangeTree.org 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:

<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
 
Scott Fell, EE MVEDeveloperCommented:
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
 
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 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
 
schwientekdAuthor Commented:
The hidden form variable and this line was exactly what I was missing.  A+

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

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now