Search code examples
javascriptphpif-statementpostreload

Reload does not change POST state


I have a problem where I want to reload the page on submit. I did this with the simple script function shown below. However, the echo'd "hello" does not disappear when I reload. Is there a reload function that changes the state of my submit post, and the "hello" is no longer appearing.

<!DOCTYPE html>
<html lang="en">
<body>
<form method="post">
    <button class="btn-login" type="submit" name="submit">Sign in</button>
</form>

<?php
if (isset($_POST['submit'])) {
    echo "hello";
    echo "<script>window.location.reload(true)<script>";
}

Solution

  • If you want to redirect to a new page (or even the same page) but without POST values, then use

    window.location.replace(url)

    https://developer.mozilla.org/en-US/docs/Web/API/Location/replace