The fact is that after sending a post request, the script takes the sent data, checks it for correctness (validity, required data, etc.) and gives an answer - either the operation was successful, or an error occurred and a list of errors (for example: error - The “name” field is not filled in. On the sending page, the corresponding message is already displayed: sending is successful or not successful.

So is there an elegant option to prevent the form from being sent again? For example, I got the idea that if you catch a page update event in javascript (if there is one to cancel it and just follow the current link. Or if you can delete any information about the request on the server after receiving the POST data, it will not be sent again as data deleted?

And a redirect is not an option. Since after the redirect, I can not send the answer script.

And fraud with an additional field with a unique value stored at the session and their further verification is also an extreme option.

What do you have any options?

  • By the way, Ajax is also an extreme option)) - Shuhratjon Jumaev

5 answers 5

To be clear, this is a question of the level of the multiplication table in web programming. And thinking like "doing a redirect or not doing it" is just the same as if talking about "two and two equal to four is not an option. I need five."

After a POST request, there should always be a redirect. Point. Go to the division in the column.

And a redirect is not an option. Since after the redirect, I can not send the answer script.

The answer, if you really want to pass it, is written to the session, and after a redirect from there it is shown.

However, on most modern websites (including this one), the form is submitted using AJAX technology, which does not require reloading the page.

  • one
    I do not agree with you that "After the POST request, there should always be a redirect. Point." Of course, I understood that you are an ooooochen intelligent person who once again equates this question to 2 + 2, and that I am very dumb that I think 2 + 2 = 5. But you yourself contradict yourself saying that "After the POST request, there should always be a redirect. Point." And then you say that it turns out there is also ajax which solves the problem without a redirect. Does this mean not a redirect? @ Ipatiev - Shuhratjon Jumaev
  • 2
    @ShuhratjonJumaev when sending a form to AJAX, the problem simply ceases to exist, since the "Refresh" button repeats the last synchronous request, which the sending of the form is not. If this is an “extreme option” for you, then yes, period. And not because there are no other solutions, but because of the possible ones, this is the simplest, and therefore the simplest in maintenance, debugging, and refinement. - D-side

If you need to know for sure whether the server will receive the client's data - try submitting via Ajax - if you save correctly and translate where necessary. If the request is not successful, you can immediately show the client the correct error.

for example something like

var jqxhr = $.post("example.php") .success(function() { alert("Успешное выполнение"); }) .error(function() { alert("Ошибка выполнения"); }) .complete(function() { alert("Завершение выполнения"); }); 

in the callbacks you need to get the necessary information that came from the server.

Ie the answer is not to submit the form directly and send it via asynchronously via Ajax. - The data will not be sent again when rebooting

link

    On my website, I solved the problem by replacing the Location while saving the result:

     header('Location: ' . $url); 

      The solution to the problem was the recording of errors in the session, after the redirect and after the conclusion of errors from the session and immediately remove it.
      Since I use codeigniter, codeigniter will be used in the solution code, but I think the essence will be clear to everyone.
      Error recording example:

        $phone_number = $this->input->post('phone_number'); if (is_numeric($phone_number) && strlen($phone_number) == 9 && (int)$phone_number > 100000000) $value = $phone_number; else { $this->session->set_userdata('msg', array('ok' => 0, 'msg' => "Неверный формат номера телефона.<br>Номер должен указываться без пробелов, знаков тире или других знаков.<br>Например: 987654321", 'type' => "warning")); redirect('/user/config'); } 

      An example of catching, withdrawing and deleting an error from a session:

       if ($this->session->has_userdata('msg')) { $msg = $this->session->userdata('msg'); $this->session->unset_userdata('msg'); if (count($msg) && strlen($msg['msg']) > 0) { echo '<div class="alert alert-' .$msg['type'] .' alert-dismissible" role="alert" style="margin: 20px;"><button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>'.$msg['msg'].'</div>'; } } 
         header( "Refresh:5; url='#', true, 303); 

        will show the message for 5 seconds.