Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    New Coder
    Join Date
    Sep 2002
    Posts
    60
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Internet Explorer Get Rid of QueryString When Posting Form

    Assume that I come to the following URL by clicking a link in, say, Home.html:

    http://www.myserver.com/Page1.asp?root=yes

    Note the querystring ?root=yes in the above URL.

    The Form in Page1.asp has many buttons. When any of the buttons is clicked, the Form gets posted & after doing the needful, comes back to Page1.asp. Note that I am not using the <input type=submit> element; I am using the simple <input type=button> element.

    All the buttons call different JavaScript functions when any of them is clicked. All these functions have the

    document.forms[0].submit()

    line which I am using to submit the Form when any button is clicked.

    The problem is since I come to Page1.asp with the querystring root=yes already appended to it when I visit this page for the first time (by clicking a link in Home.html), whenever any button is clicked to post the Form, it retains the querystring i.e. after submitting the Form, I am taken back to

    http://www.myserver.com/Page1.asp?root=yes

    but I want to get rid of the querystring when a button is clicked & the Form gets submitted i.e. after the Form gets posted, I want the URL to be

    http://www.myserver.com/Page1.asp

    & not

    http://www.myserver.com/Page1.asp?root=yes

    which is what is happening currently.

    Is there any way to get rid of the querystring when the Form gets posted?

    I tried changing the document.forms[0].submit() line to document.forms[0].submit("Page1.asp") but that doesn't make any difference. The querystring still remains appended to the URL after any of the buttons is clicked to post the Form.

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,640
    Thanks
    0
    Thanked 649 Times in 639 Posts
    What is the action on the form tag? That's what gets run when you submit a form. Maybe you need to update the action attribute using Javascript.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #3
    Senior Coder
    Join Date
    Nov 2006
    Posts
    1,000
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You could try a server side redirect if there is a query string. I think

    print $q->redirect("Page1.asp");

    is how it would be done in Perl with CGI.pm

    Does

    response.redirect("Page1.asp")

    work in ASP?

    I've never done it so this is just an educated guess. :)

    david_kw

  • #4
    New Coder
    Join Date
    Sep 2002
    Posts
    60
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Stephen, I haven't set any action on the Form tag. When I am using document.forms[0].submit() to submit the Form, what's the need to specify the action on the Form tag?

    Also I have specified in my post that I am not using the <input type=submit> element to submit the Form; rather I am using <input type=button> to submit the Form. So, if I am not mistaken, setting the action on the Form tag will any how not make any difference, isn't it? Please correct me if I am wrong.

    David, I would like to post the Form using document.forms[0].submit() only as my app basically relies heavily on JavaScript. I don't want to use any server-side redirection. I need to incorporate lots & pots of changes if I have to change the redirection process using ASP since the app had been coded by someone else. It's just that the original coder left & I have being given the task to finish it.

    BTW, is there any way to change the method of a Form (from POST to GET or from GET to POST) using JavaScript? I tried using

    document.forms[0].method='post'

    but that doesn't make any difference. The querystring still remains appended to the URL. I added the above line just before the document.forms[0].submit() line.

    I also tried

    document.forms[0].action="Page1.asp'

    but here too, the querystring remains appended to the URL.

    Please help me out......I am looking out for a solution desperately.

  • #5
    Senior Coder
    Join Date
    Nov 2006
    Posts
    1,000
    Thanks
    0
    Thanked 0 Times in 0 Posts
    My understanding is that all form submits in html are posts. It's not clear to me exactly why the query string gets appended to the URL in that case other than to reflect the state change.

    Unfortunately I'm at the end of my knowledge of the subject as I'm not aware of anything in javascript that would let you change the current address. Good luck.

    david_kw

  • #6
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Quote Originally Posted by arpan_de View Post
    Stephen, I haven't set any action on the Form tag. When I am using document.forms[0].submit() to submit the Form, what's the need to specify the action on the Form tag?
    The action attribute isn't optional, it's required. It should always exist in the HTML source code of a form and contain the URI of the target form handler.
    BTW, is there any way to change the method of a Form (from POST to GET or from GET to POST) using JavaScript? I tried using

    document.forms[0].method='post'

    but that doesn't make any difference. The querystring still remains appended to the URL.
    It shouldn't make any difference since the attribute should be case insensitive, but try using "POST" instead of "post". If that doesn't work, specify it in the HTML source instead.
    Quote Originally Posted by david_kw View Post
    My understanding is that all form submits in html are posts. It's not clear to me exactly why the query string gets appended to the URL in that case other than to reflect the state change.
    Nope. The default method used is GET. Only if the method is explicitly specified as POST will that method be used.
    Last edited by liorean; 01-10-2007 at 09:41 PM.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #7
    Senior Coder
    Join Date
    Nov 2006
    Posts
    1,000
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by liorean View Post
    The default method used is GET. Only if the method is explicitly specified as POST will that method be used.
    I stand corrected. Not that I really doubted but I put this together to help me understand. Maybe it'll help. (Google doesn't handle it when it is a post but the theory should be sound)

    Code:
    <html>
      <head>
        <title>Form Post Test</title>
        <script type="text/javascript">
          function postIt() {
            document.f.method = "post";
            document.f.submit();
          }
          function getIt() {
            document.f.submit();
          }
        </script>
      </head>
      <body>
        <div>
          <form name="f" method="get" action="http://www.google.com/search">
            <input type="text" name="q" />
            <input type="hidden" name="hl" value="en" />
          </form>
          <button onclick="postIt();">Post it</button>
          <button onclick="getIt();">Get it</button>
        </div>
      </body>
    </html>
    Note, you can see my form sets the method to "get" and the postIt overwrites that to a "post".

    david_kw

  • #8
    New Coder
    Join Date
    Sep 2002
    Posts
    60
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by liorean
    The action attribute isn't optional, it's required. It should always exist in the HTML source code of a form and contain the URI of the target form handler.
    liorean, the action attribute isn't a must in the Form tag; it is optional. If the action is not specified, then the page posts to itself when the Form is submitted. Try out this simple HTML code:
    Code:
    <html>
    <body>
    <form method=post>
    <input type=text name="txt">
    <input type=submit value="SUBMIT">
    </form>
    </body>
    </html>
    Anyway, friends, I have taken care of the problem by adopting a simpler server-side approach.

    Thanks to one & all for your inputs.

    Regards,

    Arpan

  • #9
    Senior Coder
    Join Date
    Nov 2006
    Posts
    1,000
    Thanks
    0
    Thanked 0 Times in 0 Posts
    liorean is right from what I know. Action is required in a form tag for html 4.

    http://www.w3.org/TR/html4/interact/forms.html#h-17.3

  • #10
    Senior Coder
    Join Date
    Nov 2006
    Posts
    1,000
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hmm I can't edit for some reason so consider this part of the previous post. =P

    Code:
    action      %URI;          #REQUIRED -- server-side form handler --
    david_kw

  • #11
    Senior Coder
    Join Date
    Nov 2006
    Posts
    1,000
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Of course from my experience it doesn't stop working. It just won't validate.

    (I hate it when edit won't work =P)

  • #12
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Quote Originally Posted by arpan_de View Post
    liorean, the action attribute isn't a must in the Form tag; it is optional. If the action is not specified, then the page posts to itself when the Form is submitted.
    That only works because your browser has nonstandard behaviour to cover for HTML programmer mistakes. That doesn't mean it works in every other browser on every other platform, so don't count on it. If the specification says something is REQUIRED, then it's a really bad idea to leave it out unless you're indifferent to whether it works or not.
    Anyway, friends, I have taken care of the problem by adopting a simpler server-side approach.
    Nice to hear it's been solved.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •