Domanda

I have a form which I'd like to post to the same page. The form action contains a fragment identifier for the element on the page that contains it - what I want it to do is post the data, then reload the same page at the same point so the user doesn't need to scroll down.

example index.php:

<div>
  some very long content that pushes my form down the page
</div>
<div id="myDiv">
  <form action="index.php#myDiv" method="post">
    <input name="abc" value="123"/>
    <input type="submit" value="submit"/>
  </form>
</div>

However I have a couple of problems - firstly I'm not sure if this is good practice and can't find much on what is considered good practice with fragment identifiers.

Second, some others have said that sometimes the form "jumps to the top" when they click submit. I haven't been able to reproduce this yet but it seems pretty obvious that instead of posting the data, the browser is going with the fragment identifier action instead.

Can anyone suggest a better way to do this?

È stato utile?

Soluzione

Redirect the user after the form submission using header() function along with identifier.

<div>
  some very long content that pushes my form down the page
</div>
/*Use header() to redirect the person on the #point*/
<?php
//form data goes here, if it passes everything then use
header('Location:index.php#myDiv');
exit;
?>
<div id="myDiv">
  <form action="index.php" method="post">
    <input name="abc" value="123"/>
    <input type="submit" value="submit"/>
  </form>
</div>
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top