Submit() in javascript

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • NawazAhmed
    New Member
    • Feb 2008
    • 36

    Submit() in javascript

    Hi,
    Is it possible to get the page1's form values to page2. I know about submit button and submit function but when I am trying to get the values in page 2 with the help of Request.form("F ieldName") it gives me error: Request is undefined.
    Can anyone help me with this.
    My code is something like this:
    Page1:
    in html:
    [HTML]<INPUT id="FieldName" maxLength="35" name="FieldName ">
    <input id="Button1" onclick="javasc ript:Submit()" type="button" value="Continue " name="Button1">
    [/HTML]
    the javascript function is:
    [CODE=javascript]function Submit() {
    document.forms[0].submit();
    window.location .href = "page2.aspx "
    }
    [/CODE]
    Page2:
    vbscript:
    x = Request.Form("F ieldName")

    Thanks.
    Last edited by acoder; Feb 28 '08, 08:15 AM. Reason: Added code tags
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Don't set the location.href if you're submitting. Set the action property of the form instead to this location.

    Comment

    • NawazAhmed
      New Member
      • Feb 2008
      • 36

      #3
      Thanks for the Reply,
      As you set I tried that also. If my form is
      <form id="frm1" name="frm1" action="page2.a spx" method="post" runat="server">

      and the button is

      <input id="Button1" onclick="javasc ript:Submit()" type="button" value="Continue " name="Button1">

      and javascript is
      document.forms[0].submit();

      It submits the form but wont forward to the next page.

      Waiting for your reply,
      Thanks.

      Originally posted by acoder
      Don't set the location.href if you're submitting. Set the action property of the form instead to this location.

      Comment

      • hsriat
        Recognized Expert Top Contributor
        • Jan 2008
        • 1653

        #4
        In your code, you are submitting the form to page2.aspx and then redirecting it to the same page, ie. page2.aspx. If that is the case, then you don't need to redirect it.

        But if you need to redirect it to some other page, then after processing the form data in page2.aspx, add this to the end of your ASP code...
        Code:
        Response.AddHeader("Location","page3.aspx");

        Comment

        • NawazAhmed
          New Member
          • Feb 2008
          • 36

          #5
          I am sorry if I am not clear, Iam new to XML.
          So from your message what I understand is if you mention the action in form tab then it directs you to the mentioned page(in action). But its not doing that.
          Its submitting the text but not forwarding to the next page and if I am transfering it to next page(with the statement window.location .href = "page2.aspx "). there I cannot able to get the submitted value from the previous page as I said in the 1'st post.

          Any suggestions.... .....


          Originally posted by hsriat
          In your code, you are submitting the form to page2.aspx and then redirecting it to the same page, ie. page2.aspx. If that is the case, then you don't need to redirect it.

          But if you need to redirect it to some other page, then after processing the form data in page2.aspx, add this to the end of your ASP code...
          Code:
          Response.AddHeader("Location","page3.aspx");

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            Originally posted by NawazAhmed
            Its submitting the text but not forwarding to the next page
            If it submits the text, it should submit to the action page. Which pagedo you want to forward to?

            Comment

            • NawazAhmed
              New Member
              • Feb 2008
              • 36

              #7
              I want to direct it to page 2 and its not directing automatically so I am using
              window.location .href = "page2.aspx " in the javascript of page1.aspx
              But the problem is when I am tring to retrieve page1 form's data by using the statement Request.Form("F ieldNameofPage1 'sControl") its gives me error:
              Request is undefined.

              Originally posted by acoder
              If it submits the text, it should submit to the action page. Which pagedo you want to forward to?

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #8
                So that means the form doesn't submit.

                Post the client-side code (load the page and view source in the browser).

                Comment

                • hsriat
                  Recognized Expert Top Contributor
                  • Jan 2008
                  • 1653

                  #9
                  Instead of [html]<input id="Button1" onclick="javasc ript:Submit()" type="button" value="Continue " name="Button1">[/html]try this....[html]<input id="Button1" type="submit" value="Continue " name="Button1"> </input>[/html]

                  PS: Is there any special reason to use onclick="javasc ript:Submit()"?

                  Comment

                  • NawazAhmed
                    New Member
                    • Feb 2008
                    • 36

                    #10
                    Page1:
                    source:
                    [HTML]<HTML>
                    <Head>
                    <script Language=javasc ript src="Page1.js"> </script>
                    </Head>
                    <body>
                    <form id="Form1" method="post" action="Page2.a spx">
                    <DIV style="DISPLAY: inline; Z-INDEX: 101; LEFT: 56px; WIDTH: 112px; POSITION: absolute; TOP: 120px; HEIGHT: 24px">License Number</DIV>
                    <INPUT style="Z-INDEX: 103; LEFT: 192px; POSITION: absolute; TOP: 120px" type="text" name="txtName">
                    <INPUT style="Z-INDEX: 102; LEFT: 152px; POSITION: absolute; TOP: 168px" type="button" value="Button" onclick="javasc ript submit();">
                    </form>
                    </body>
                    </HTML>
                    [/HTML]
                    Page1.js:
                    [CODE=javascript]Function submit(){
                    document.form.s ubmit();
                    window.location .href = "Page2.aspx " // If I remove this line page1 is not
                    // forwarding to Page2
                    }
                    [/CODE]
                    Page2.aspx
                    [HTML]<HTML>
                    <Head>
                    <script Language=javasc ript >
                    var x = Request.Form("t xtName");
                    alert(x.value);
                    </script>
                    </Head>
                    <body>
                    </body>
                    </HTML>
                    [/HTML]
                    According to the above code the answer should be name entered by the user on the first page

                    If I use the button type=submit and in form tab action="page2.a spx" method = "post", its not going to the next page, even if it goes to the next page getting the error Request is undefined.

                    Thanks.
                    Originally posted by acoder
                    So that means the form doesn't submit.

                    Post the client-side code (load the page and view source in the browser).
                    Last edited by acoder; Mar 4 '08, 11:15 AM. Reason: Added code tags

                    Comment

                    • NawazAhmed
                      New Member
                      • Feb 2008
                      • 36

                      #11
                      Hi hsriat,
                      I tried type=submit, problem with this is it won't forward to the next page when u specify action="page2.a spx",
                      and why I am using onclick="javasc ript:Submit()" is bcz I have to validate the fields on the form and then submit and move to the next page.
                      I already posted the code, why don't u try once
                      If you find something please lemme know.

                      Thanks

                      Originally posted by hsriat
                      Instead of [html]<input id="Button1" onclick="javasc ript:Submit()" type="button" value="Continue " name="Button1">[/html]try this....[html]<input id="Button1" type="submit" value="Continue " name="Button1"> </input>[/html]

                      PS: Is there any special reason to use onclick="javasc ript:Submit()"?

                      Comment

                      • hsriat
                        Recognized Expert Top Contributor
                        • Jan 2008
                        • 1653

                        #12
                        Didn't you try the common way of validation?
                        ie.[html]<form action="action. php" method="get | post" onsubmit="retur n validate()">
                        <!--
                        form inputs
                        -->
                        <input type="submit" name="submit" value="Continue "></input>
                        </form>[/html]See if this helps you.

                        Comment

                        • NawazAhmed
                          New Member
                          • Feb 2008
                          • 36

                          #13
                          I tried my problem is not with submission, my major problem is Request.Form
                          This statement is the main cause of error.
                          Accordingly this staement should return the fields value whatever we are calling but it won't recognize Request. The error is Request is undefined.
                          Do I have to include any system files or inhert something?????

                          Thanks.
                          Originally posted by hsriat
                          Didn't you try the common way of validation?
                          ie.[html]<form action="action. php" method="get | post" onsubmit="retur n validate()">
                          <!--
                          form inputs
                          -->
                          <input type="submit" name="submit" value="Continue "></input>
                          </form>[/html]See if this helps you.

                          Comment

                          • acoder
                            Recognized Expert MVP
                            • Nov 2006
                            • 16032

                            #14
                            NawazAhmed, please enclose your posted code in [code] tags (See How to Ask a Question).

                            This makes it easier for our Experts to read and understand it. Failing to do so creates extra work for the moderators, thus wasting resources, otherwise available to answer the members' questions.

                            Please use [code] tags in future. Thanks!

                            Moderator.

                            Comment

                            • acoder
                              Recognized Expert MVP
                              • Nov 2006
                              • 16032

                              #15
                              Originally posted by NawazAhmed
                              Page2.aspx
                              [HTML]<HTML>
                              <Head>
                              <script Language=javasc ript >
                              var x = Request.Form("t xtName");
                              alert(x.value);
                              </script>
                              </Head>
                              <body>
                              </body>
                              </HTML>
                              [/HTML]
                              Request.Form should be within server-side tags. JavaScript will not recognise it.

                              Comment

                              Working...