0

i have a Button, that executes php Code after click(I am testing something with EMails and everything is working so far, except this Issue with the "PHP Script loading after Refresh" Problem):

HTML:

<form method="post"> 
<input type="submit" name="button1"
        class="button" value="Button1" /> 

<input type="submit" name="button2"
        class="button" value="Button2" /> 
</form>

PHP:

<?php
    if(array_key_exists('button1', $_POST)) { 
        button1(); 
    } 
    else if(array_key_exists('button2', $_POST)) { 
        button2(); 
    } 
    function button1() { 
        echo "mail"; 
    } 
    function button2() {
            ini_set( 'display_errors', 1 );
            error_reporting( E_ALL );
            $from = "test@example.de";
            $to = "thisIsA@secret.de";
            $subject = "Betreff";
            $message = "Text";
            $headers = "From:" . $from;
            mail($to,$subject,$message, $headers);
            echo "Test email sent";
    } 
?>

When i first go to the Site, everything is working and the script is not executing, before I press one of the Buttons. Thats perfect -> BUT when I refresh the Page, the script gets executed without me pressing the Button.

How can I prevent the script loading after refresh?

yama_HD
  • 480
  • 1
  • 4
  • 14
  • Check this. Meta tag should help https://stackoverflow.com/questions/10643626/refresh-page-after-form-submitting/14667451 – DuhVir Apr 16 '20 at 11:46
  • use session, or GET+redirect – Kristian Apr 16 '20 at 11:54
  • Thanks @DuhVir `header("Refresh: 0"); // here 0 is in seconds` worked for me but thats not totally what i wanted, if I refresh the Page, then everything insertet might get Lost :/. – yama_HD Apr 16 '20 at 12:14
  • @yama_HD ok. Than look at this https://www.yourhowto.net/detect-page-refresh-php/ session(varian 2) is a good choise, like said Kristian above – DuhVir Apr 16 '20 at 12:28

1 Answers1

1

Showing that kind of message is often called "flash message": showing a message that appear once, and not showing again after refresh.

Your problem comes from browser's behavior: if a page is loaded from a POST request, then if user hit refresh button, it will make a new POST request with same POST parameter

But what you want is: if a page is loaded from a POST request, then if user hit refresh button, it will make a new GET request (of course: without any POST parameter)

It can be achieved by using session, the step is as follows:

  • user click a button (1 or 2)

  • POST to self button1=Button1 or button2=Button2

  • Page processing the request (do the mail function)

  • Page redirect / refresh to show the new message that previously saved in session variable.


<?php
    session_start();

    if(array_key_exists('button1', $_POST)) {
        button1();
    }
    else if(array_key_exists('button2', $_POST)) {
        button2();
    }
    function button1() {
        $_SESSION["msg"] = "mail";
        echo "<script>window.location = window.location.pathname;</script>";
        exit();
    }
    function button2() {
        ini_set( 'display_errors', 1 );
        error_reporting( E_ALL );
        $from = "test@example.de";
        $to = "thisIsA@secret.de";
        $subject = "Betreff";
        $message = "Text";
        $headers = "From:" . $from;
        mail($to,$subject,$message, $headers);
        $_SESSION["msg"] = "Test email sent";
        echo "<script>window.location = window.location.pathname;</script>";
        exit();
    }

?>
<form method="POST">
<input type="submit" name="button1"
        class="button" value="Button1" />

<input type="submit" name="button2"
        class="button" value="Button2" />
</form>

<?php
if(!empty($_SESSION["msg"])){
    echo $_SESSION["msg"];
    $_SESSION["msg"] = null;
    unset($_SESSION["msg"]);
}
?>

Kristian
  • 2,456
  • 8
  • 23
  • 23