Home php How do I avoid resubmitting the form on update?

How do I avoid resubmitting the form on update?

Author

Date

Category

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

So is there an elegant way to prevent the form from being resubmitted?
For example, I got the idea that if I catch the page refresh event in javascript (if there is one, cancel it and just follow the current link.
Or if it is possible on the server, after receiving the POST request data, to delete some information about the request, maybe it will not be re-sent since the data has been deleted?

Also, a redirect is not an option. Since after the redirect, I cannot transmit the script response.

And also manipulations with an additional field with a unique value saved in the session and their further verification is also an extreme option.

Do you have any options?


Answer 1, authority 100%

To be clear, this is a multiplication table level question in web programming. And thinking like “redirect or not” is like thinking “twice two equals four is not an option. I need five”.

There should always be a redirect after a POST request. Point. Moving on to long division.

Also, redirecting is not an option. Since after the redirect, I cannot transmit the script’s response.

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

However, on most modern sites (including this one), form submission is done using AJAX technology, which does not require a page reload.


Answer 2, authority 10%

If you need to know exactly whether the server will accept the client’s data, try submitting via Ajax, if you save it correctly and translate it where you need it. 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 ("Successful execution");})
 .error (function () {alert ("Execution error");})
 .complete (function () {alert ("Completion of execution");});

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

That is, the answer is not to submit the form directly, but send it asynchronously via Ajax. – Data will not be re-parsed on reboot

link


Answer 3

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

header ('Location:'. $ url);

Answer 4

The solution to the issue was to record errors in the session, after the redirect and after outputting errors from the session and immediately delete it.
Since I am using codeigniter in the solution code, codeigniter will be used, but I think the essence will be clear to everyone.
An example of an error record:

$ phone_number = $ this- & gt; input- & gt; post ('phone_number');
    if (is_numeric ($ phone_number) & amp; & amp; strlen ($ phone_number) == 9 & amp; & amp; (int) $ phone_number & gt; 100000000) $ value = $ phone_number;
    else {
$ this- & gt; session- & gt; set_userdata ('msg', array ('ok' = & gt; 0, 'msg' = & gt; "Invalid phone number format. & lt; br & gt; The number must be specified without spaces, dashes or other characters. & lt; br & gt; For example: 987654321 ", 'type' = & gt;" warning "));
      redirect ('/ user / config');
    }

An example of catching, displaying and removing an error from a session:

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

Answer 5

header ("Refresh: 5; url = '#', true, 303);

will show the message for 5 seconds.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions