identify when the form is sent by the Submit button

Asked

Viewed 458 times

1

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

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

You have some way of handling it?

Follow the 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>
  • In fact the method was yes POST, because the return only repeated the last action that had been a POST. Maybe you can set a field by clicking Ubmit that allows you to identify if the call came from click or not. As the return will submit the same value, you can set the date/ time, something so that helps you identify that was an authentic post by the button. Just a few ideas that might help you.

  • I could put together an example with the date and time?

  • @Hugoborges, usually when you receive a form in the back-end you should process it and at the end of it give a redirect to the user, preventing it press F5 or turn the page and forward the form.

  • @jlHertel the problem and that I can not use the redirect because it has to be using before any out-put and I run some echo before submitting the form.

  • 1

    @Hugoborges, I think you don’t understand the flow. The correct one would be: 1 - Shows page with form. 2 - Received the POST form, processes and at the end of the redirect to another page, can be the form again. If in the middle of processing the form you have echo, you can use the function ob_start to prevent the exit of the controls.

  • @jlHertel thanks for the help, took a look at the documentation of the ob_start and solved 100% of my problems. Thank you

  • @jlHertel ok. I used the // Inicializa o buffer de saida&#xA;ob_start(); at the beginning of the archive and the // Descarrega o conteúdo interno do buffer&#xA;ob_end_flush(); at the end, that way I can use the redirect wherever I want.

Show 2 more comments

1 answer

3


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

Usually I follow the following flow:

  1. I process something and assemble a form for the user
  2. I receive the POST form in the backend
  3. Process the received form
  4. I redirect the user, sometimes to a listing, sometimes back to the same page with form.

It may be that in step 3 you do echo or print in the middle of your code. Or even, 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 function ob_start.

This question describes very well the use of function.

  • a question arose, how to display a message of success and only then execute the redirect?

  • Usually for these cases you save in the user session the success/error messages and show on the next page. As soon as the message is shown, you remove again from the session, not to show twice.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.