How do I keep a website page at the same point it was before updating?

Asked

Viewed 3,116 times

4

I have a page in html and php and put a form at the bottom of the page.

When I click on the "send" button the page refreshes and goes up.

This way the user has to scroll all the time until arriving downstairs again to see if the data has been registered, if there is an error message ...

 <form id="formulario" name="login" method="POST" 
 action="<?php echo $_SERVER["REQUEST_URI"];?>"> 

In the action I tried "PHP_SELF' too.

The structure is html, body and a div, the form is inside the div.

  • 1

    Can show the action of form and the basic structure of HTML?

  • 1

    It is possible to use a fragment or do you have any requirements that you don’t? In my view, this is one of his duties.

  • @brasofilo add ai in question

  • 1

    @Viníciusgobboa.deOliveira I’m not used to using Ragment, do you think this can solve it? Then I’ll research.

2 answers

2


Can be solved with a anchor just before the <form>:

<a name="form-anchor"></a>
<form ETC...

And make the action of the form go to that anchor:

action="<?php echo $_SERVER["REQUEST_URI"];?>#form-anchor"

I tested with this PHP:

<html>
<head>
    <style>
    #div-grande {
        background-color:#ddd;
        min-height: 3800px;
    }
    </style>
</head>
<body>
    <div id="div-grande">
        <?php if( isset( $_POST['grafico'] ) ) echo 'POSTED<br />'; ?>
        Dummy 3800px height
    </div>
    <a name="form-anchor"></a>
    <form method="post" action="<?php echo $_SERVER["REQUEST_URI"];?>#form-anchor">
         <input type="submit" name="grafico" value="Enviar">
    </form>
</body>
</html>
  • Basic thing, but I do not always use there does not come in the mind nor with the wear. Thank you very much and excuse for asking such simple thing.

  • 1

    It’s from the time of Windows 95 :)

  • But it works perfectly, I just put the anchor further down because I have a lot of text before the form. Again it paid off. It’s because I’m working with database and I have to leave the interface "nice" with CSS, then I get angry....

  • One day I will be "psychopath" like you... because I remember the anchor and I know that you have to put in that specific place inside the action is Froid,?

  • Now I know too. :)

  • 1

    He’s got the makings of the movie Stomach? Num tava ligado, não, mas agora eu tou! ;)

Show 1 more comment

1

One way to do it is this:

Add a parameter to the form sending URL. For example the parameter ? form=1

Add an id to the form, for example #formulario. Based on these settings in php put this code:

if ($_GET["form"] == "1") { 
    echo '<script> 
        jQuery(document).ready(function(){
            function scrollToAnchor("#formulario"){ 
                jQuery(\'html,body\').animate({scrollTop: jQuery(aid).offset().top - 119}, 700);       
            }           
            scrollToAnchor("#'.$_GET["url"].'"); 
        });
      </script>';  
}

As soon as php checks that the url has the form parameter it will add a JS that will scroll the page (right to animation) until the div that is the form.

NOTE: I’m imagining that you use the jQuery library.

  • Actually I only use javascript in last case, but thank you very much, @brasofilo gave me what I was looking for. Thing that we see when we are studying html, but that at the time of suffocation...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.