[RESOLVED] Submit form, with out going to the top of the page [HTML]
Results 1 to 4 of 4

Thread: [RESOLVED] Submit form, with out going to the top of the page [HTML]

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Posts
    9

    resolved [RESOLVED] Submit form, with out going to the top of the page [HTML]

    Is there a way to keep the page from going back to the top when the user clicks a form post button? I have a 'notes' section on my site for the user, about 1/2 way down the site - but I just want it to make the SQL query when they click submit, and print "Notes updated.". It does this just fine, but it scrolls to the top of the page, which is really annoying; Is there a way to make it so it doesn't do this?

  2. #2
    Pedantic Curmudgeon Weedpacket's Avatar
    Join Date
    Aug 2002
    Location
    General Systems Vehicle "Thrilled To Be Here"
    Posts
    21,885
    Based only on what you have supplied, I'm going to guess that you're not updating the same page, you're leaving the page and going to a new copy as the result of a submit. In that case a pretty simple approach would be to add an id to the bit of the page you want to be at the top, and include it in the URL you're submitting to by putting it on the end after a "#".
    THERE IS AS YET INSUFFICIENT DATA FOR A MEANINGFUL ANSWER
    FAQs! FAQs! FAQs! Most forums have them!
    Search - Debugging 101 - Collected Solutions - General Guidelines - Getting help at all

  3. #3
    Senior Member
    Join Date
    Mar 2009
    Posts
    812
    Submitting the form actually reloads the page, which is what's causing this illusion of jumping to the top of the page. There are 2 things you can do to counteract this.

    1) Similar to what Weedpacket suggested, place an anchor tag with a specific name on your page where you want the page "to be" after you submit the form. In the form's action attribute, append #whatever_the_anchor_tag_is.
    For example, if the anchor was <a name="notes"></a> then you'd want to append #notes.

    2) A little fancier (and more involved) is to use JavaScript (namely jQuery) to make an AJAX call that does what ever the form does. Using this with event.preventDefault() will stop the page from "jumping" to the top.

    Good luck!
    Declare variables, not war.

  4. #4
    Pedantic Curmudgeon Weedpacket's Avatar
    Join Date
    Aug 2002
    Location
    General Systems Vehicle "Thrilled To Be Here"
    Posts
    21,885
    Quote Originally Posted by Bonesnap
    1) ...
    That is what I suggested, except that the "id" attribute is usually a better choice than "name". First, "id" can be used on any element, so you don't need a spurious "<a name='notes'></a>" element. Second, the "name" attribute is absent from the <a> tag in HTML5. See the discussion in
    http://www.w3.org/TR/html4/struct/li...nchors-with-id (and when it says "older user agents", remember it was written last century; the id attribute didn't exist before HTML4), and the specification in of the <a> element in http://www.w3.org/TR/html5/text-leve...#the-a-element
    Last edited by Weedpacket; 01-03-2013 at 06:11 PM.
    THERE IS AS YET INSUFFICIENT DATA FOR A MEANINGFUL ANSWER
    FAQs! FAQs! FAQs! Most forums have them!
    Search - Debugging 101 - Collected Solutions - General Guidelines - Getting help at all

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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