- This topic is empty.
-
AuthorPosts
-
May 20, 2014 at 5:32 pm #170742
chrisburton
ParticipantIs there a way to not show the form resubmission alert on refresh? I’m also trying to stick to one php file.
May 20, 2014 at 6:19 pm #170746__
ParticipantYou mean the browser warning? It happens because the browser recognizes that you’re POSTing the same info you already POSTed.
There’s a trick, it’s simple but the idea is little hard to follow:
Say the user submits
value=hello
to yourpost.php
page. That means the POST and the page will be associated in the browser’s history. (And so when refreshing or backing up, it warns you that you’re submitting the form again: this is what causes the problem.)The solution is to make sure that, when the user submits the form, the page that it is associated with is never returned: if it is never returned, it won’t be in the browser history, so the user will never be able to refresh/backup to it.
You can do this with a redirect (i.e.,
Location
header).I’ve found the simplest approach is:
- when you get a POST (form submission), save the POST data to the session.
- Do not output (print) anything.
- send a
Location
header, redirecting back to the same page. - when the session has POST data in it, you know there’s a form submission to be processed.
- from this point, you can continue processing the form and printing the output as normal.
outline:
<?php session_start(); if( $_POST ){ // put form submission in session $_SESSION["POST"] = $_POST; // redirect and stop script immediately header( "Location: http://example.com/this/page.php" ); exit; } if( isset( $_SESSION["POST"] ) ){ // oooh! a form submission! // pretend it was in POST all along $_POST = $_SESSION["POST"]; unset( $_SESSION["POST"] ); } // continue processing/output as normal
May 20, 2014 at 6:45 pm #170748chrisburton
ParticipantOooh! Nice. Let me give that a shot.
May 20, 2014 at 6:56 pm #170749chrisburton
Participant@traq Hmm. Having trouble on the last bit.
from this point, you can continue processing the form and printing the output as normal.
How can I process the form into a database if it’s unset? Use
$_POST
again?May 20, 2014 at 7:24 pm #170751chrisburton
ParticipantNevermind. I can just do this:
if($_POST){ $_SESSION['POST'] = $_POST; header('Location: http://something.com'); exit; } if(isset($_SESSION['POST'])) { $_POST = $_SESSION['POST']; unset($_SESSION['POST']); } if(!empty($_POST)) { //do database stuff here
May 20, 2014 at 8:05 pm #170753__
ParticipantHow can I process the form into a database if it’s unset? Use $_POST again?
Yup, that’s what I meant (intended). Should’ve been more clear.
This can get more complicated; if you’re uploading files, for example. But it’s the same idea.
May 20, 2014 at 8:30 pm #170757chrisburton
ParticipantThanks for the help on this one @traq. After researching on SO, I only found methods by either using two separate files or AJAX. I didn’t want to use either of those options so this helped a lot.
May 20, 2014 at 8:46 pm #170760__
ParticipantNo problem. I went looking for the article that originally “clued me in” to this issue+solution, but it was years ago (and, as I recall, was a long, difficult search in the first place). Oh well. I’ve memorized it.
May 21, 2014 at 8:41 am #170802chrisburton
Participantand, as I recall, was a long, difficult search in the first place
I’m a bit surprised this isn’t a well documented solution. Oh and I’m going to use this for my Kirby Comment System as well.
May 21, 2014 at 10:09 am #170807__
ParticipantOh and I’m going to use this for my Kirby Comment System as well.
Whup, well, now I want royalties : p
I’m a bit surprised this isn’t a well documented solution.
Hmm. Maybe I should document it…
-
AuthorPosts
- The forum ‘Back End’ is closed to new topics and replies.