the Future of the Web
  • Home
  • Hire Us
  • Articles
  • About
  • Contact
  • Redirecting after POST

    May 23 2007

    When working with forms, we have to think about what will happen when someone clicks back or forward or refresh. For example, if you submit a form and right afterwards refresh the page, the browser will ask if you want to resend the data (usually in a pretty long alert box talking about making purchases).

    People don't always read alert boxes, and often get used to clicking OK all the time (I know I fall in this category), so sometimes comments and other things get submitted more than once.

    To solve this, you can simply do an HTTP redirect after processing the POST data. This is possible with any server-side language, but in PHP it would look something like this:

    if (count($_POST)) {
        // process the POST data
        add_comment($_POST);
    
        // redirect to the same page without the POST data
        header("Location: ".$_SERVER['PHP_SELF']);
        die;
    }
    

    This example assumes that you process the form data on the same page that you actually want to go to after submitting. You could just as easily redirect to a second landing page.

    On this site, on each blog post page, I have a form that submits to the same blog post page. After processing the comment, I send a redirect again to the same page. If you add a comment and then refresh, or click back and then forward, the comment won't be submitted twice. (However, if you click back and then click Add Comment again, it will. I really should filter out duplicates, but that's another topic.)

    This works because you essentially replace a POST request with a GET request. Your browser knows that POST requests are not supposed to be cached, and that you should be warned before repeating a POST request. After the redirect, the page is the result of a simple GET request. Refreshing the page simply reloads the GET request, leaving the POST request lost between the pages in your browser history.

    Tags: server post get rest
    View 15 Comments | Add a comment
  • Comments

    1. dave at 6:31am on June 22, 2007

    wonderful. i have been doing self-redirect after post for along time and i can say that this is the single most important use of redirect!! thank you for your clear explanation! im sure it will help many

    2. _ck_ at 2:21pm on August 4, 2007

    The only problem with this technique is that if you are using a CMS/blog that has to preload all it's code before a template is displayed, you essentially are causing twice the load because you are loading it twice (once for processing the post data, then the second time when you externally redirect back to itself).

    Multiply this by a hundred people doing it at once on a busy site and your server will feel it.

    I've am digging into the problem and it occurs to me there might be a way to clear post data on the client-side via javascript. Would cause zero extra server load. More research needed!

    3. nash at 9:05am on September 14, 2007

    nice explanation. but this is what i get when i use this. ive been getting it before and i thought this would be a solution. it didnt solve it though. please let me knw how u can do that.
    many thanx in advance:

    Warning: Cannot modify header information - headers already sent by (output started at some-url-page.php:10) in myfilename.php on line XX

    4. Jesse Skinner at 11:36am on September 14, 2007

    @nash - Sounds like the problem is some HTML or text has been outputted before you do header("Location") - and I bet that happened on line 10 of some-url-page.php.

    Just make sure the header() function is called before anything gets outputted.

    5. Jesse at 9:01pm on November 8, 2007

    I want to use this method, and I know I've successfully used it in the past.  But the problem I'm having now happens after the POST data is handled -- when the "Location: " header is sent, Firefox prompts me about whether I want to re-send my post data to the GET destination (which I don't of course - that's the problem I'm trying to avoid!) and the page goes blank if I answer 'No'.  This is not a problem in IE.  Any ideas? 

    My POST operation has the action "/home" and my redirect looks like "/home/eID/[some_id]".  Thanks in advance!

    6. Manoj Sonawane at 5:41am on November 28, 2007

    i want redirct page to itself when some one change to and add process to this page

    7. jauco at 2:33pm on December 10, 2007

    this is of course begging for a test ;-)

    But seriously, I was thinking about using GWT just to avoid this (major) problem of postdata.

    8. jauco at 2:35pm on December 10, 2007

    and this solution seems much better, thanks!

    (It apparently doesn't stop you from pressing the submit button to early though)

    9. vicky at 2:42pm on January 5, 2008

    Hi,I have doubt in jsp..i need to redirect the same page after inserting the data in to database....can u help me....Before that it ll be d inserted successfully,no need dis type of method.....

    10. Jesse Skinner at 3:47pm on January 5, 2008

    @vicky - I'm not that familiar with JSP, but a quick Google search for 'jsp redirect' gives the following example:

    String redirectURL = "http://hostname.com/";
    response.sendRedirect(redirectURL);

    So you'd replace the redirectURL with the URL of the page. I hope that helps!

    11. Bob at 1:50pm on February 3, 2008

    Great solution of this annoying problem! Thank you.

    12. vicky at 7:34am on February 13, 2008

    Hi jesse...i had one doubt...In(JSP & HTML) List out box,four options r there,i used option tag for select dat but its inserting in d database.....But mine doubt is, i have to select Multi more option in dat List out box,I donno wat option s dat...Can u clarify mine doubt(JSP)

    13. Prabath at 10:38am on April 1, 2008

    Although this didn't directly help me solving my "Clearing post data when back to the original page", it helped me to device a method to solve the problem.

    Thank you very much for your valuable teachings.

    14. Patrick Moore at 8:44pm on April 22, 2008

    @ck : Assuming you aren't parsing scores of variables, you could pass along the same information as GET variables to be interpreted by the destination.

    In example, a blog comment form:

    if (count($_POST)) {
        // process the POST data
        add_comment($_POST);

        // Collect and pass through variables
        $subject=$_POST["subject"];
        $name=$_POST["name"];
        $url_append="?subj=$subject&name=$name";

        // redirect to the same page without the POST data but with the variables
        header("Location: ".$_SERVER['PHP_SELF'].$url_append);
        die;
    }

    if ($_GET["subj"] && $_GET["name"]) {
        // Read in the variables we passed via the URL
        $subj=$_GET["subj"];
        $name=$_GET["name"];

        // Notify the user of action
        echo "Thank you, $name.  Your comment ($subj) has been recorded.";
    }

    15. Patrick Moore at 8:47pm on April 22, 2008

    An afterthought: You could set cookies for bulkier or session variables for sensitive data to be interpreted at the destination page.  Yes it would increase server load some, but wouldn't require duplicate database queries... and cookies would incur little to no extra server load other than parsing variables.

    Add a Comment

    Note: HTML tags and entities will be converted so that they are displayed as you type them. This means if you type in <em>, people will see <em>, and if you type &lt;em&gt;, people will see &lt;em&gt;.

  • Request a Quote

  • Jesse Skinner

    Jesse Skinner
    • Hire Me
    • About Me
    • Email Me
    • RSS Feed RSS Icon
  • Recent Articles

    • Parse Accept-Language to detect a user's language
    • Twitter
    • Three years of The Future of the Web
    • Saving data to a file with PHP
    • Easy web scraping with PHP
    • See all the articles
    • IBM: Where and when to use Ajax
    • Code Igniter 1.6.0 Released
    • Update a Dev Site Automatically with Subversion
    • JavaScript Functions are Variables
    • See All...
  • Categories

    • javascript (37)
    • links (19)
    • about (18)
    • web (14)
    • server (10)
    • html (10)
    • css (8)
    • carnival (7)
    • browsers (7)
    • design (4)
    • seo (4)
    • ads (4)
    • standards (4)
    • events (4)
    • work (4)
  • Older Articles

    • May 2008
    • April 2008
    • February 2008
    • January 2008
    • December 2007
    • November 2007
    • September 2007
    • August 2007
    • July 2007
    • June 2007
    • May 2007
    • April 2007
    • March 2007
    • February 2007
    • January 2007
    • December 2006
    • November 2006
    • October 2006
    • September 2006
    • August 2006
    • July 2006
    • June 2006
    • May 2006
    • April 2006
    • March 2006
    • February 2006
    • January 2006
    • December 2005
    • November 2005
    • October 2005
    • September 2005
    • August 2005
    • April 2005
    • See All...
Copyright © 2008 Jesse Skinner | CSS | XHTML | RSS | Help | Impressum | Cutie Quilts | Internet Blog Top Sites