Adamhumbug Posted October 21, 2023 Share Posted October 21, 2023 My application requires the page to reload alot to trigger all of the functions that calculate price and i am now starting to see the following message alot. To display this page, Firefox Developer Edition must send information that will repeat any action (such as a search or order confirmation) that was performed earlier. I know that i can use AJAX to only reload sections that i have changed, but in effect i am changing so much of the page that i felt it more simple to reload the page. Is there a simple but 'professional' way that i can prevent this message and prevent things being added to the database more than once when a reload is required? Quote Link to comment https://forums.phpfreaks.com/topic/317389-refreshing-the-page-resending-the-action/ Share on other sites More sharing options...
Barand Posted October 21, 2023 Share Posted October 21, 2023 Send a location header to same page so it reload without the posted data myscript.php: if ($_SERVER['REQUEST_METHOD']=='POST') { // validate POST data if (no errors) { // update database header("Location: myscript.php"); exit; } } // build page content Quote Link to comment https://forums.phpfreaks.com/topic/317389-refreshing-the-page-resending-the-action/#findComment-1612500 Share on other sites More sharing options...
Adamhumbug Posted October 22, 2023 Author Share Posted October 22, 2023 (edited) The URLs has got quite a lot of params in them. Would header("Refresh:0"); yield the same result? This is currently how i am doing everything: function applyDiscountToAllDiscountableQuoteItems($quoteId, $discountPercentage) { $remainingPercentage = 100 - $discountPercentage $.ajax({ type: 'post', data: { 'ajax': 'applyDiscountToAllDiscountableQuoteItems', 'quoteId': $quoteId, 'discountPercentage': $discountPercentage, 'remainingPercentage': $remainingPercentage }, success: function(resp) { location.reload(); } }) } function setNewItemPrice($newPrice, $itemId) { $.ajax({ type: 'post', data: { 'ajax': 'setNewItemPrice', 'quoteId': $quoteId, 'newPrice': $newPrice, 'itemId': $itemId }, success: function(resp) { // location.reload(); } }) } Edited October 22, 2023 by Adamhumbug Quote Link to comment https://forums.phpfreaks.com/topic/317389-refreshing-the-page-resending-the-action/#findComment-1612502 Share on other sites More sharing options...
Adamhumbug Posted October 22, 2023 Author Share Posted October 22, 2023 11 hours ago, Barand said: Send a location header to same page so it reload without the posted data myscript.php: if ($_SERVER['REQUEST_METHOD']=='POST') { // validate POST data if (no errors) { // update database header("Location: myscript.php"); exit; } } // build page content So in one of my function (in functions.php file) that is called with ajax from other page i should put header("Location: functions.php"); Quote Link to comment https://forums.phpfreaks.com/topic/317389-refreshing-the-page-resending-the-action/#findComment-1612503 Share on other sites More sharing options...
Adamhumbug Posted October 22, 2023 Author Share Posted October 22, 2023 This seems to do the trick - or is this a horrible way of doing it. if (window.history.replaceState) { window.history.replaceState(null, null, window.location.href); } window.location = window.location.href; Quote Link to comment https://forums.phpfreaks.com/topic/317389-refreshing-the-page-resending-the-action/#findComment-1612504 Share on other sites More sharing options...
mac_gyver Posted October 22, 2023 Share Posted October 22, 2023 (edited) the message you cited occurs when you do a 'view source' of the page in the browser. any get parameters you have in the URL should only determine what content is gotten and displayed on the page, not what post method form action is performed in the server-side code. if the last request the browser has for a URL is a html post method form submission, the form data will get resubmitted when you do a 'view source' of the page (even if you have prevented the form itself from being redisplayed on that page.) your first post in this thread indicated that you know you could use ajax, not that you were already using it. the reply you got is based on how you would normally do this, using a html post method form submitted to server-side post method form processing code, on the same page, to prevent the browser from trying to resubmit the post method form data should that page get reloaded, a 'view source' performed on it, or the URL is browsed away from and back to, by instructing the browser to perform a get request for the exact (including any existing get parameters) same URL of the current page. the main point of using ajax on a web page is so that you don't reload the whole page. if what you are doing means that you are going to reload the whole page anyways, you shouldn't be using ajax to do this. when you do use ajax to submit a post method form, the ajax response code needs to deal with any server-side success/failure response (validation errors, duplicate data errors, ...) that are produced and output by the server-side code. Edited October 22, 2023 by mac_gyver Quote Link to comment https://forums.phpfreaks.com/topic/317389-refreshing-the-page-resending-the-action/#findComment-1612508 Share on other sites More sharing options...
Adamhumbug Posted October 23, 2023 Author Share Posted October 23, 2023 On 10/22/2023 at 3:28 PM, mac_gyver said: the message you cited occurs when you do a 'view source' of the page in the browser. any get parameters you have in the URL should only determine what content is gotten and displayed on the page, not what post method form action is performed in the server-side code. if the last request the browser has for a URL is a html post method form submission, the form data will get resubmitted when you do a 'view source' of the page (even if you have prevented the form itself from being redisplayed on that page.) your first post in this thread indicated that you know you could use ajax, not that you were already using it. the reply you got is based on how you would normally do this, using a html post method form submitted to server-side post method form processing code, on the same page, to prevent the browser from trying to resubmit the post method form data should that page get reloaded, a 'view source' performed on it, or the URL is browsed away from and back to, by instructing the browser to perform a get request for the exact (including any existing get parameters) same URL of the current page. the main point of using ajax on a web page is so that you don't reload the whole page. if what you are doing means that you are going to reload the whole page anyways, you shouldn't be using ajax to do this. when you do use ajax to submit a post method form, the ajax response code needs to deal with any server-side success/failure response (validation errors, duplicate data errors, ...) that are produced and output by the server-side code. So this should only be happening becuase i have inspector open at the same time? I will do some testing around this. I have noted your other comments, which i will come back to. Quote Link to comment https://forums.phpfreaks.com/topic/317389-refreshing-the-page-resending-the-action/#findComment-1612529 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.