identify when the form is sent by the submit button

1

Well I've put an example of a very simple form in php. I have to identify when it is sent by the button and when it is sent when the user returns the page.

The problem is, if you submit the form 2 times and then click the back button it says that the method was POST, it was not because the user clicked back.

Is there any way to handle this?

Here's a simple example.

<?php
session_start();

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $txt = "POST";
} else {
    $txt = "GET";
}

echo $txt;
?>


<br><br><br>
<form action="form.php" method="post">
    <input type="text" name="ae" value="ae" />
    <input type="submit" name="enviar" value="enviar" />
</form>
    
asked by anonymous 23.03.2017 / 11:55

1 answer

3

When receiving forms on the server it is always interesting to redirect the user to the end of the processing so that he can not press the refresh key in the browser.

Normally I follow the following flow:

  • Process something and set up a form for the user
  • I get the POST of the form in the backend
  • Process the form received
  • I redirect the user, sometimes to a listing, sometimes back to the same page with form.
  • It may be that in step 3 you make echo or print in the middle of your code. Or, your code can give some Notice or Warning .

    To ensure that there will be no problems in redirect, it is always interesting to use the output buffer with the ob_start .

    This describes the function very well.

        
    23.03.2017 / 13:53