Maintain scroll position after submit

Im using this as the basis for my site. ContactUsing the link, I set up a contact form with PHP verification to check if the user filled out the fields correctly. After clicking "Submit":

  • a verification message is displayed (for example, "Sorry, your name is a required field", etc.) or
  • โ€œThank you for filling out the formโ€ message is displayed (if all fields are filled in properly).

However, the page then jumps to the first page, displaying the "About" page. If you go to the Contact page, you will see messages. This is very annoying.

An example of my attempts can be found here :

I start in this field, and any hint or clue where to look further will be greatly appreciated.

+4
source share
2 answers

I would redirect the original URL by adding a hash to the URL that will tell your site where to go.

When executed comment.php, it will respond with a 302 redirect with attached #contact. Say your page is being index.htmlredirected to

/index.html#contact

About redirection in PHP: http://php.net/manual/en/function.header.php

In index.htmlyou should check the hash and do what you do when you click the link:

window.onload = function(){
   if (location.hash === "contact") {
      goto('#contact', this);
   }
}

, , . index.php index.php #contact. PHP.

<form action="/index.php#contact" method="POST" ...>
  ...
</form>

1- : Javascript ( , script). , . #<name> id="<name>". CSS, onload.

: hash , goto(), , "".

+2

Source: https://habr.com/ru/post/1545554/


All Articles