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 = "[email protected]";
            $to = "[email protected]";
            $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?

4
  • Check this. Meta tag should help stackoverflow.com/questions/10643626/… Commented Apr 16, 2020 at 11:46
  • use session, or GET+redirect Commented Apr 16, 2020 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 :/. Commented Apr 16, 2020 at 12:14
  • @yama_HD ok. Than look at this yourhowto.net/detect-page-refresh-php session(varian 2) is a good choise, like said Kristian above Commented Apr 16, 2020 at 12:28

1 Answer 1

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 = "[email protected]";
        $to = "[email protected]";
        $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"]);
}
?>

Sign up to request clarification or add additional context in comments.

3 Comments

Well its working now but my Inputs are no longer there because the page refreshed, thats not soo good
then either use ajax, or just save the input in another session variable
@mickmackusa thanks, it's an answer long ago that I haven't checked. Answer edited

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.