1
I have a problem on my site, when I click save inside a modal, it sends the data via Ajax and makes the insertion in the database, when performing these steps, I call a javascript function to update the page window.location.reload();
but when you refresh the page, you are inserting twice in the database, the POST data is reentering and entering the insertion if.
Is there any way to clear these variables so you don’t go inside if for insertion when updating the page ? I’ve tried using unset
but I was unsuccessful.
I used window.location.href and also had no success.
My code below:
if(isset($_POST['act2'])) $act2 = $_POST['act2'];
if(isset($act2)){
...//Aqui faz o processo de inserção no banco
}
This $act2 is coming through AJAX, as when blocking losing the data the $act2, without it coming as POST again, or how to update the page without it repeats the insertion as the POST data is sent again when updating.
Try using php to redirect, replacing the
window.location.reload();
forheader('Location:mesmapagina.php');
die();
Source: https://stackoverflow.com/a/4870934/4734177 (Other possible solutions exist in the linked question)– gustavox
Just a question... If you are using ajax for what reason are you wanting to update the page after the request? Are you doing any different action to the standard behavior of
form
? Here is a solution I found: https://www.kodementor.com/prevent-duplicate-form-submission-on-refresh-in-php/– Victor Carnaval
The idea of asynchronous requests is precisely to avoid reloading the entire page, but only in specific areas
– Vinicius De Jesus
When I click close the modal tab, I am calling a javascript function to update the page, when I update, the form redoes the submission... The method to insert is in Ajax but to list the content of my table, is not in ajax, why I update when closing the modal.
– Felipe Pacheco
You have to show the Ajax and the form. Only with this information is it difficult to say anything.
– Sam