Hello!

Tell me how to do better.

There is a form validated by javascript. After the data is pushed into variables and the POST request for the * .php file flies away, there is a check in the file for the presence of a variable in the _POST array, if there are variables, it is written to the MySQL database. The problem is that if you update the page on which the user arrives, the data in MySQL is written again. Tell me how you can organize a check so that when you refresh the page, the data will not be recorded again. For what it is needed, there is a feedback form on the site, the user types a review, indicates a name, a review, attaches a photo. Further, the data is validated and flies to another file, in another file they are expanded in variable form, the ID and moderation flag are hung.

What are my thoughts: Add some data to the table that can be pulled and the user's PC, let's say IP, let's say a browser cast. Then compare.

Please do not offer AJAX, it has not yet been studied TT. But if you insist and say that it is very easy and cool, then nothing remains.

  • @Mike and what's a comment, not an answer? - jekaby
  • @jekaby Wrote in response. although the answer should be more detailed, but not ready ... - Mike

2 answers 2

  1. When receiving a POST request, write to the database and, instead of the finished page, give the user a redirect so that he comes back with GET.
  2. Check for duplicate information on all (or part of) values. If it is not possible to pre-generate some kind of unique ID, and the gene will be generated every time the form is opened, regardless of whether it will post later or not. And we write this value with post to the database along with the data and check its uniqueness by it.

For a guaranteed result, the second point is necessary. One first 100% guarantee will not give.

  • Yes, this is a solution with an intermediate file, the user arrives there with a POST request, the data is shoved into MySQL, then the user quickly flies away further))) With the ID, the solution is adequate, I’ll think how you can make it so that the ID is generated based on some features of the device the user gets on a site, thanks for the answer. - Alex
  • @ Alex There, each time the form is opened, the next value from the sequence is obtained (the oracle replacement to the auto-increment columns mysql), yes, the id pile flies to nowhere, but if you make bigint it will not be very crowded soon. I'm not very good. I like the idea of ​​something attached to the visitor, it’s necessary to ensure uniqueness, and if he decides to create two different records ... - Mike

If you are using AJAX, then just make a reset form. If you are using AJAX, then just make a reset form.

$.ajax({ url: 'you_url', data:$('#formid').serialize(), method:'POST', success: function(r){ alert('hallelujah'); $('#formid')[0].reset(); } }); 

The process is as follows. Validating, sending the backend, when receiving a positive response, discard the form and inform the person that everything is cool. If he decides to refresh the page, he will see an empty form.

  • Yes, now, unfortunately, there will be no time to implement this functionality using AJAX, but I will definitely return to this in a week. Thank you, the code is very clear to me, I will develop your answer. Thanks again. - Alex