2

In an .aspx page I'm filling out a number of hidden form fields with JavaScript and then want to post the form to another .aspx page. It submits the form properly but when I look at the Request.Params["org"] in the page that was called in the form's action it's null.

<script src="prototype.js"></script>
<script>
function doIt() {
    $('org').value = org_int;
    $('frmCheckout').submit();
}
</script>
...
<form id="frmCheckout" method="post" action="checkout.aspx">
<input type="hidden" value="" id="org" name="org">
<input type="submit" onclick="doIt()" value="Submit">
</form>

$ is the prototype notation which was brought in before the example. $ in prototype is the same as document.getElementById('frmCheckout').

MAJOR EDIT 2/1/2021 7:52: Looking for the solution I found a couple of new bits of information but it still doesn't work.

I found PostBackUrl which will change the .aspx's action to another .aspx page. This part works. When submitted, it calls Checkout.aspx

<asp:Button ID="Submit" PostBackUrl="Checkout.aspx" runat="server" Text="Submit" />

But the data is not available in Checkout.aspx... there are no values in Request.Form (which there is supposed to be) or Request.Params.
I've also tried:

<asp:TextBox runat="server" name="jsonString" id="jsonString" value="js" />

But I'm getting an invalid __VIEWSTATE I know I'm supposed to change it but nothing has worked yet.

Velocedge
  • 1,222
  • 1
  • 11
  • 35
  • js can not transfer data from one page to another. session or cookie may help you to this. https://stackoverflow.com/questions/17502071/transfer-data-from-one-html-file-to-another – Murat Seker Feb 01 '21 at 18:55
  • Temporarily make your hidden element a standard text box. Does it display your expected value before you submit? – Jasen Feb 01 '21 at 19:18
  • Jasen: I had been checking the value in debugger but made it a text box to make sure and it's showing the data. Murat: I have literally hundreds of pages that fill form fields with JS and post to classic asp pages... this is the first time with .aspx. So, what's different about .aspx? – Velocedge Feb 01 '21 at 19:29
  • Are your values actually getting posted? Check with F12. – mxmissile Feb 01 '21 at 21:16
  • 1
    Using the debugger (F12) to verify that the proper values are being put into the form fields... they are. Target .aspx gets the post but no values there. – Velocedge Feb 01 '21 at 21:20
  • Try `Request["org"]` instead of `Request.Params["org"]`, I assume you are checking these parameters in `checkout.aspx`'s code behind? – mxmissile Feb 01 '21 at 21:27
  • LOL... only checked a few thousand times!.. Request["org"], Request.Form["org"], Request.Params["org"]. Keep thinking it will show up sooner or later. ;-) – Velocedge Feb 01 '21 at 22:40
  • The original code you posted is really just HTML and JavaScript. You could just place it in a file named "default.html". Are you using Visual Studio? If so, what version and what type of project did you create? Posting the entire code for your demo project's .aspx and.aspx.cs files would be helpful as well. – Tu deschizi eu inchid Feb 02 '21 at 14:48

4 Answers4

1

Try the following. In the following example the Javascript needs to be after the form code. Request.Form["org"] is used to get the value of "org" from the posted data. Also, if one uses $ in Javascript, it's important not to use it until it's defined. See javascript dollar sign variable not working.

Test.aspx (works)

<!-- directives -->
<% @Page Language="C#" %>

<html>
   <head> 
      <title> Test Form </title> 
   </head>
   
   <body>
      <h3> Test Form </h3>

      <form runat="server" id="frmCheckout" method="post" action="checkout.aspx">
         <input runat="server" type="hidden" id="org" name="org" value=""/>
         
      </form>

      <script>
          function setInitialVals() {
              document.getElementById('org').value = "3";
              document.getElementById('frmCheckout').submit();
          };

          setInitialVals();
      </script>
   </body>
</html>

checkout.aspx

<!-- directives -->
<% @Page Language="C#" %>

<html>
   <head> 
      <title> Checkout</title> 
   </head>
   
   <body>
      <h3> Checkout </h3>
      
       <% Response.Write("org: " + Request.Form["org"]); %>  
      
   </body>
</html>

The following won't work:

Test.aspx (doesn't work)

Note: The following won't work because the Javascript is executed before the input element is rendered.

<!-- directives -->
<% @Page Language="C#" %>

<html>
   <head> 
      <title> Test Form </title> 
   </head>
   
   <body>
      <h3> Test Form </h3>

      <script>
          function setInitialVals() {
              document.getElementById('org').value = "3";
              document.getElementById('frmCheckout').submit();
          };

          setInitialVals();
      </script>

      <form runat="server" id="frmCheckout" method="post" action="checkout.aspx">
         <input runat="server" type="hidden" id="org" name="org" value=""/>
         
      </form>
   </body>
</html>

Resources

Introducing ASP.NET Web Pages - HTML Form Basics

Tu deschizi eu inchid
  • 4,117
  • 3
  • 13
  • 24
  • I edited the example. I was trying to paint a simple picture trying to focus on the post data not being received, but I confused the issue. Prototype ($) is defined for it's used. – Velocedge Feb 01 '21 at 21:09
1

Please, change the approach. Consider add an asp button into the form and fire click event for this button; the button properties need to be like this topic of Microsoft Docs How to: Post ASP.NET Web Pages to a Different Page:

<script src="prototype.js"></script>
<script>
function doIt() {
    $('#org').value = org_int;
    $('#btnPostBack').click();
}
</script>
...
<form id="frmCheckout" method="post">
<input type="hidden" value="" id="org" name="org">
<input type="button" onclick="doIt()" value="Submit">
<asp:Button ID="btnPostBack" ClientIdMode="Static" PostBackUrl="~/checkout.aspx"  runat="server" Text="Post Form To Another Page" style="display:none;"/>
</form>

Note: I saw in your code that id's selectors not contains hashtag prefixes in your JS code...

Antonio Leonardo
  • 1,805
  • 1
  • 8
  • 18
  • I have tried the – Velocedge Feb 02 '21 at 19:07
  • I use both prototypejs and jQuery. prototype uses $('name') where jQuery uses $('#name'). There is no problem with $('org') = org_int. If I were using JQuery here, that would be $('#org').val(org_int). When I mix them I use $j('#org') – Velocedge Feb 02 '21 at 19:09
0

Please,

try change form method attribute from post to get, if you want use "org" as Query String:

$('org').value = org_int;
$('frmCheckout').submit();
...
<form id="frmCheckout" method="get" action="checkout.aspx">
<input type="hidden" value="" id="org" name="org">
</form>
Antonio Leonardo
  • 1,805
  • 1
  • 8
  • 18
  • Actually, there are a lot more parameters. I just showed this one for the concept. GET does work fine but POST is a little safer and supports virtually unlimited size. – Velocedge Feb 01 '21 at 20:59
  • Ok. I beleive the "Request.Params" only works from current aspx to current code behind; to cross pages, you need to use Request["org"] to get data (please, if possible, validate this in debug mode). – Antonio Leonardo Feb 02 '21 at 12:31
  • I've tried Request, Request.Params, Request.Form, and the data just isn't being posted. – Velocedge Feb 02 '21 at 14:59
0

Update:

I downloaded prototype.js and added it to the test code below which seems to work. The only difference between the code you posted and the code below should be that I hard-coded the value of "org_int" in function "doIt": let org_int = 3;.

test.aspx

<% @Page Language="C#" %>

<html>
   <head> 
      <title> Test Form </title> 
   </head>
   
   <body>
      <h3> Test Form </h3>

       <script src="prototype.js"></script>
             
       <script>
           function doIt() {

               try {
                        let org_int = 3;

                        $('org').value = org_int;
                        $('frmCheckout').submit();

                        //document.getElementById('org').value = org_int;
                        //document.getElementById('frmCheckout').submit();
               }
               catch (err) {
                   console.log(err);
                   alert(err);       
               }
           }

       </script>

        <form id="frmCheckout" method="post" action="checkout.aspx">
            <input type="hidden" value="" id="org" name="org">
            <input type="submit" onclick="doIt()" value="Submit">
        </form>
       
   </body>
</html>

checkout.aspx

<% @Page Language="C#" %>

<html>
   <head> 
      <title> Checkout</title> 
   </head>
   
   <body>
      <h3> Checkout </h3>
      
       <% Response.Write("Request.Form[\"org\"]: " + Request.Form["org"] + "<BR>"); %>  
       <% Response.Write("Request.Params[\"org\"]: " + Request.Params["org"]); %>  
      
   </body>
   
</html>
Tu deschizi eu inchid
  • 4,117
  • 3
  • 13
  • 24
  • As I said in the edited post the $ is "prototype"... like jQuery and it's the way they reference elements same as getElementById. It's defined in "prototype.js" before the regular script tag. – Velocedge Feb 01 '21 at 23:54
  • If you haven't already tried the simple test code I posted, you may want to give it a try. – Tu deschizi eu inchid Feb 02 '21 at 00:55
  • User9938... re-read my last post about where the $ comes from. Your test post has not included prototype.js. Then you can check http://prototypejs.org/ if you want more details. The $ is definitely NOT the problem. – Velocedge Feb 02 '21 at 02:53