Clearing POST DATA for Browser Refresh

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • tmax

    Clearing POST DATA for Browser Refresh

    PHP Pros:

    I have a simple html form that submits data to a php script, which
    processes it, and then redisplays the same page, but with a "thank you"
    message in place of the html form. This is all working fine. However,
    when refresh the browser, I get the following message displayed:

    "The page you are trying to view contains POSTDATA. If you resend the
    data, any action the form carried out (such as as search or online
    purchse) will be repeated. To resend the data, click OK. Otherwise,
    click Cancel."

    Obviously I don't want my users to resend the data to me.

    What do I need to do code-wise so that when the browser is refreshed,
    the page is reloaded without this message being displayed.

    Thanks in advance.
  • David Haynes

    #2
    Re: Clearing POST DATA for Browser Refresh

    tmax wrote:[color=blue]
    > PHP Pros:
    >
    > I have a simple html form that submits data to a php script, which
    > processes it, and then redisplays the same page, but with a "thank you"
    > message in place of the html form. This is all working fine. However,
    > when refresh the browser, I get the following message displayed:
    >
    > "The page you are trying to view contains POSTDATA. If you resend the
    > data, any action the form carried out (such as as search or online
    > purchse) will be repeated. To resend the data, click OK. Otherwise,
    > click Cancel."
    >
    > Obviously I don't want my users to resend the data to me.
    >
    > What do I need to do code-wise so that when the browser is refreshed,
    > the page is reloaded without this message being displayed.
    >
    > Thanks in advance.[/color]
    Don't redirect to the form; redirect to a summary page without the form
    or make your form page sensitive to whether the $_POST data is available
    and do not have it emit a <form> if the $_POST data is available.

    I prefer the first method over the second.

    -david-

    Comment

    • d

      #3
      Re: Clearing POST DATA for Browser Refresh

      "tmax" <tmax@comcast.n et> wrote in message
      news:8281b$43f2 1506$481a8e8e$1 8008@PGTV.COM.. .[color=blue]
      > PHP Pros:
      >
      > I have a simple html form that submits data to a php script, which
      > processes it, and then redisplays the same page, but with a "thank you"
      > message in place of the html form. This is all working fine. However,
      > when refresh the browser, I get the following message displayed:
      >
      > "The page you are trying to view contains POSTDATA. If you resend the
      > data, any action the form carried out (such as as search or online
      > purchse) will be repeated. To resend the data, click OK. Otherwise, click
      > Cancel."
      >
      > Obviously I don't want my users to resend the data to me.
      >
      > What do I need to do code-wise so that when the browser is refreshed, the
      > page is reloaded without this message being displayed.
      >
      > Thanks in advance.[/color]

      In the script to which the form submits, store the $_POST data somewhere
      else, say a session. Then, use this:

      header("Locatio n: http://host/path/to/script.php");
      exit();

      to bounce the user to another page. Most browsers know to not re-submit the
      post data to this new page. The new script can even be the originating
      one - as long as you use the location: header, you'll be fine. You can then
      access the submitted data wherever you stored it.

      PS. if you're using a session to store the data, call session_write_c lose()
      before the header() call, otherwise nasty things happen on some browsers.

      dave


      Comment

      • frizzle

        #4
        Re: Clearing POST DATA for Browser Refresh


        d wrote:[color=blue]
        > "tmax" <tmax@comcast.n et> wrote in message
        > news:8281b$43f2 1506$481a8e8e$1 8008@PGTV.COM.. .[color=green]
        > > PHP Pros:
        > >
        > > I have a simple html form that submits data to a php script, which
        > > processes it, and then redisplays the same page, but with a "thank you"
        > > message in place of the html form. This is all working fine. However,
        > > when refresh the browser, I get the following message displayed:
        > >
        > > "The page you are trying to view contains POSTDATA. If you resend the
        > > data, any action the form carried out (such as as search or online
        > > purchse) will be repeated. To resend the data, click OK. Otherwise, click
        > > Cancel."
        > >
        > > Obviously I don't want my users to resend the data to me.
        > >
        > > What do I need to do code-wise so that when the browser is refreshed, the
        > > page is reloaded without this message being displayed.
        > >
        > > Thanks in advance.[/color]
        >
        > In the script to which the form submits, store the $_POST data somewhere
        > else, say a session. Then, use this:
        >
        > header("Locatio n: http://host/path/to/script.php");
        > exit();
        >
        > to bounce the user to another page. Most browsers know to not re-submit the
        > post data to this new page. The new script can even be the originating
        > one - as long as you use the location: header, you'll be fine. You can then
        > access the submitted data wherever you stored it.
        >
        > PS. if you're using a session to store the data, call session_write_c lose()
        > before the header() call, otherwise nasty things happen on some browsers.
        >
        > dave[/color]

        Just curious, but is there / would you recommend a good alternative for
        the Sessions?

        Frizzle.

        Comment

        • tmax

          #5
          Re: Clearing POST DATA for Browser Refresh

          Well, I've tried everything that has been suggested so far - thanks for
          everyone's input.

          However, let me restate the problems differently as I believe it wasn't
          correct to begin with.

          I have a file: index.php

          index.php contains a form, which when submitted for processing, calls
          itself using <form method="POST" action="{$_SERV ER['PHP_SELF']}">

          When the form is submitted and index.php is reevaluated, I have the
          following IF statement: (FYI: 'submit' is the name of the submit button.)
          ------------
          IF ( !isset($_POST['submit']) ) {
          // The form has NOT been SUBMITTED - display the form

          echo <<<htmloutput
          <form method="POST" action="{$_SERV ER['PHP_SELF']}">
          ...
          </form>
          htmloutput;

          } ELSE {
          // The form was SUBMITTED - process the form

          process the form data, etc. and print in PLACE of the form the
          following:

          echo "Informatio n submitted... Thank you.";
          }
          -------------

          I understand I can redirect to a (different) summary page, but all I
          want to do is replace the form with the summary message. The problem
          comes into being when, after the summary message is displayed, the user
          goes to refresh the browser. The browser is still caching the form data
          for resubmission. I've tried things like: unset($_POST) or
          unset($_POST['submit']) (and all the other form fields) but that has no
          affect.

          Any other suggestions?

          Thanks in advance.



          d wrote:[color=blue]
          > "tmax" <tmax@comcast.n et> wrote in message
          > news:8281b$43f2 1506$481a8e8e$1 8008@PGTV.COM.. .
          >[color=green]
          >>PHP Pros:
          >>
          >>I have a simple html form that submits data to a php script, which
          >>processes it, and then redisplays the same page, but with a "thank you"
          >>message in place of the html form. This is all working fine. However,
          >>when refresh the browser, I get the following message displayed:
          >>
          >>"The page you are trying to view contains POSTDATA. If you resend the
          >>data, any action the form carried out (such as as search or online
          >>purchse) will be repeated. To resend the data, click OK. Otherwise, click
          >>Cancel."
          >>
          >>Obviously I don't want my users to resend the data to me.
          >>
          >>What do I need to do code-wise so that when the browser is refreshed, the
          >>page is reloaded without this message being displayed.
          >>
          >>Thanks in advance.[/color]
          >
          >
          > In the script to which the form submits, store the $_POST data somewhere
          > else, say a session. Then, use this:
          >
          > header("Locatio n: http://host/path/to/script.php");
          > exit();
          >
          > to bounce the user to another page. Most browsers know to not re-submit the
          > post data to this new page. The new script can even be the originating
          > one - as long as you use the location: header, you'll be fine. You can then
          > access the submitted data wherever you stored it.
          >
          > PS. if you're using a session to store the data, call session_write_c lose()
          > before the header() call, otherwise nasty things happen on some browsers.
          >
          > dave
          >
          >[/color]

          Comment

          • P-Rage

            #6
            Re: Clearing POST DATA for Browser Refresh

            I have to ask, why would you assume your users would refresh that page
            rather than following a link somewhere else?

            Comment

            • P-Rage

              #7
              Re: Clearing POST DATA for Browser Refresh

              I have to ask, why would you assume your users would refresh that page
              rather than following a link somewhere else?

              Comment

              • Marc van Lieshout

                #8
                Re: Clearing POST DATA for Browser Refresh

                I don't think there is another solution than storing state information
                somehwere.
                I would use the following method:

                1. Before displaying the form, generate some unique number
                2. add this number in a hidden field in the form.
                3. When the user submits, mark the number as 'used'
                4. When the user submits again, ignore the data, because the number
                has already been 'used' up.

                The simplest way to do this is to create a counter, either in a file on disk
                or in
                a database. Before you send the form to the client, increment the counter
                and send
                the counter value in a hidden field in the form.

                If you save the form data in a database table, add a 'counter' field to the
                table.
                When the form is first submitted, a record with the counter value sent with
                the form should not exist. Save data and counter.
                If the form is submitted for a second or third time, the counter is already
                present in
                your table, so you should ignore the data in the post.

                Using a session cookie is, I think, less work.


                "tmax" <tmax@comcast.n et> wrote in message
                news:8281b$43f2 1506$481a8e8e$1 8008@PGTV.COM.. .[color=blue]
                > PHP Pros:
                >
                > I have a simple html form that submits data to a php script, which
                > processes it, and then redisplays the same page, but with a "thank you"
                > message in place of the html form. This is all working fine. However,
                > when refresh the browser, I get the following message displayed:
                >
                > "The page you are trying to view contains POSTDATA. If you resend the
                > data, any action the form carried out (such as as search or online
                > purchse) will be repeated. To resend the data, click OK. Otherwise, click
                > Cancel."
                >
                > Obviously I don't want my users to resend the data to me.
                >
                > What do I need to do code-wise so that when the browser is refreshed, the
                > page is reloaded without this message being displayed.
                >
                > Thanks in advance.[/color]


                Comment

                • tmax

                  #9
                  Re: Clearing POST DATA for Browser Refresh


                  True, they could instead following some other link and decide not to
                  refresh the page. However, I would like the page to function such that
                  refreshing the page simply re-displays a new blank form (after all,
                  refreshing a page is conceptually akin to resetting.)

                  I started off on this path thinking that this format should be
                  relatively easy to code with one if-else statement. So far, it has been
                  easy to code (the form and summary message work great, with very little
                  coding). I keep thinking that there is some PHP mechanism that isn't
                  being used properly here (due to my PHP inexperience.)

                  Any suggestions?

                  P-Rage wrote:[color=blue]
                  > I have to ask, why would you assume your users would refresh that page
                  > rather than following a link somewhere else?
                  >[/color]

                  Comment

                  • d

                    #10
                    Re: Clearing POST DATA for Browser Refresh

                    "frizzle" <phpfrizzle@gma il.com> wrote in message
                    news:1139946264 .842789.257780@ o13g2000cwo.goo glegroups.com.. .[color=blue]
                    >
                    > d wrote:[color=green]
                    >> "tmax" <tmax@comcast.n et> wrote in message
                    >> news:8281b$43f2 1506$481a8e8e$1 8008@PGTV.COM.. .[color=darkred]
                    >> > PHP Pros:
                    >> >
                    >> > I have a simple html form that submits data to a php script, which
                    >> > processes it, and then redisplays the same page, but with a "thank you"
                    >> > message in place of the html form. This is all working fine. However,
                    >> > when refresh the browser, I get the following message displayed:
                    >> >
                    >> > "The page you are trying to view contains POSTDATA. If you resend the
                    >> > data, any action the form carried out (such as as search or online
                    >> > purchse) will be repeated. To resend the data, click OK. Otherwise,
                    >> > click
                    >> > Cancel."
                    >> >
                    >> > Obviously I don't want my users to resend the data to me.
                    >> >
                    >> > What do I need to do code-wise so that when the browser is refreshed,
                    >> > the
                    >> > page is reloaded without this message being displayed.
                    >> >
                    >> > Thanks in advance.[/color]
                    >>
                    >> In the script to which the form submits, store the $_POST data somewhere
                    >> else, say a session. Then, use this:
                    >>
                    >> header("Locatio n: http://host/path/to/script.php");
                    >> exit();
                    >>
                    >> to bounce the user to another page. Most browsers know to not re-submit
                    >> the
                    >> post data to this new page. The new script can even be the originating
                    >> one - as long as you use the location: header, you'll be fine. You can
                    >> then
                    >> access the submitted data wherever you stored it.
                    >>
                    >> PS. if you're using a session to store the data, call
                    >> session_write_c lose()
                    >> before the header() call, otherwise nasty things happen on some browsers.
                    >>
                    >> dave[/color]
                    >
                    > Just curious, but is there / would you recommend a good alternative for
                    > the Sessions?[/color]

                    Databases or files spring to mind :)
                    [color=blue]
                    > Frizzle.
                    >[/color]


                    Comment

                    • d

                      #11
                      Re: Clearing POST DATA for Browser Refresh

                      "tmax" <tmax@comcast.n et> wrote in message
                      news:1f9bd$43f2 3b15$481a8e8e$2 9885@PGTV.COM.. .[color=blue]
                      > Well, I've tried everything that has been suggested so far - thanks for
                      > everyone's input.
                      >
                      > However, let me restate the problems differently as I believe it wasn't
                      > correct to begin with.[/color]

                      I understand completely what you're asking :)

                      Try this code:

                      <?

                      session_start() ;

                      if (isset($_POST["data"])) {
                      $_SESSION["data"]=$_POST["data"];
                      session_write_c lose();
                      header("Locatio n: ".$_SERVER["SCRIPT_URI "]);
                      exit();
                      }

                      if (isset($_SESSIO N["data"])) {
                      ?>
                      <html>
                      <head><title>Th anks!</title></head>
                      <body>
                      Thank you for submitting your data:<br>
                      <?=$_SESSION["data"];?>
                      </body>
                      </html>
                      <?
                      } else {
                      ?>
                      <html>
                      <head><title>Pl ease Submit</title></head>
                      <body>
                      Use the form to submit your data:<br>
                      <form method="post">
                      <input type="text" name="data"><br >
                      <input type="submit" value="Go!">
                      </form>
                      </body>
                      </html>
                      <?
                      }
                      ?>

                      It will show a form, and take that posted data and store it in a session.
                      Then, the script re-directs you back to itself, and that redirection stops
                      your browser wanting to resubmit the data when refreshed. The session is
                      then checked, and if the data is present, a thank-you note is displayed. If
                      not, it then shows the initial form.

                      dave


                      Comment

                      • d

                        #12
                        Re: Clearing POST DATA for Browser Refresh

                        "tmax" <tmax@comcast.n et> wrote in message
                        news:a3153$43f2 5750$481a8e8e$2 9897@PGTV.COM.. .[color=blue]
                        >
                        > True, they could instead following some other link and decide not to
                        > refresh the page. However, I would like the page to function such that
                        > refreshing the page simply re-displays a new blank form (after all,
                        > refreshing a page is conceptually akin to resetting.)
                        >
                        > I started off on this path thinking that this format should be relatively
                        > easy to code with one if-else statement. So far, it has been easy to code
                        > (the form and summary message work great, with very little coding). I
                        > keep thinking that there is some PHP mechanism that isn't being used
                        > properly here (due to my PHP inexperience.)
                        >
                        > Any suggestions?[/color]

                        The header("Locatio n: ") command :) See my other post... ;)
                        [color=blue]
                        > P-Rage wrote:[color=green]
                        >> I have to ask, why would you assume your users would refresh that page
                        >> rather than following a link somewhere else?
                        >>[/color][/color]


                        Comment

                        • tmax

                          #13
                          Re: Clearing POST DATA for Browser Refresh

                          Thank You Dave!!!

                          This code works - I used ob_start() to get around some header() errors,
                          but after implementing that, my form and page works exactly as I
                          intended it to. Learned quite a bit from all of this too. Thanks again!

                          Travis


                          d wrote:[color=blue]
                          > "tmax" <tmax@comcast.n et> wrote in message
                          > news:1f9bd$43f2 3b15$481a8e8e$2 9885@PGTV.COM.. .
                          >[color=green]
                          >>Well, I've tried everything that has been suggested so far - thanks for
                          >>everyone's input.
                          >>
                          >>However, let me restate the problems differently as I believe it wasn't
                          >>correct to begin with.[/color]
                          >
                          >
                          > I understand completely what you're asking :)
                          >
                          > Try this code:
                          >
                          > <?
                          >
                          > session_start() ;
                          >
                          > if (isset($_POST["data"])) {
                          > $_SESSION["data"]=$_POST["data"];
                          > session_write_c lose();
                          > header("Locatio n: ".$_SERVER["SCRIPT_URI "]);
                          > exit();
                          > }
                          >
                          > if (isset($_SESSIO N["data"])) {
                          > ?>
                          > <html>
                          > <head><title>Th anks!</title></head>
                          > <body>
                          > Thank you for submitting your data:<br>
                          > <?=$_SESSION["data"];?>
                          > </body>
                          > </html>
                          > <?
                          > } else {
                          > ?>
                          > <html>
                          > <head><title>Pl ease Submit</title></head>
                          > <body>
                          > Use the form to submit your data:<br>
                          > <form method="post">
                          > <input type="text" name="data"><br >
                          > <input type="submit" value="Go!">
                          > </form>
                          > </body>
                          > </html>
                          > <?
                          > }
                          > ?>
                          >
                          > It will show a form, and take that posted data and store it in a session.
                          > Then, the script re-directs you back to itself, and that redirection stops
                          > your browser wanting to resubmit the data when refreshed. The session is
                          > then checked, and if the data is present, a thank-you note is displayed. If
                          > not, it then shows the initial form.
                          >
                          > dave
                          >
                          >[/color]

                          Comment

                          • Jerry Stuckle

                            #14
                            Re: Clearing POST DATA for Browser Refresh

                            tmax wrote:[color=blue]
                            > Thank You Dave!!!
                            >
                            > This code works - I used ob_start() to get around some header() errors,
                            > but after implementing that, my form and page works exactly as I
                            > intended it to. Learned quite a bit from all of this too. Thanks again!
                            >
                            > Travis
                            >
                            >
                            > d wrote:
                            >[color=green]
                            >> "tmax" <tmax@comcast.n et> wrote in message
                            >> news:1f9bd$43f2 3b15$481a8e8e$2 9885@PGTV.COM.. .
                            >>[color=darkred]
                            >>> Well, I've tried everything that has been suggested so far - thanks
                            >>> for everyone's input.
                            >>>
                            >>> However, let me restate the problems differently as I believe it
                            >>> wasn't correct to begin with.[/color]
                            >>
                            >>
                            >>
                            >> I understand completely what you're asking :)
                            >>
                            >> Try this code:
                            >>
                            >> <?
                            >>
                            >> session_start() ;
                            >>
                            >> if (isset($_POST["data"])) {
                            >> $_SESSION["data"]=$_POST["data"];
                            >> session_write_c lose();
                            >> header("Locatio n: ".$_SERVER["SCRIPT_URI "]);
                            >> exit();
                            >> }
                            >>
                            >> if (isset($_SESSIO N["data"])) {
                            >> ?>
                            >> <html>
                            >> <head><title>Th anks!</title></head>
                            >> <body>
                            >> Thank you for submitting your data:<br>
                            >> <?=$_SESSION["data"];?>
                            >> </body>
                            >> </html>
                            >> <?
                            >> } else {
                            >> ?>
                            >> <html>
                            >> <head><title>Pl ease Submit</title></head>
                            >> <body>
                            >> Use the form to submit your data:<br>
                            >> <form method="post">
                            >> <input type="text" name="data"><br >
                            >> <input type="submit" value="Go!">
                            >> </form>
                            >> </body>
                            >> </html>
                            >> <?
                            >> }
                            >> ?>
                            >>
                            >> It will show a form, and take that posted data and store it in a
                            >> session. Then, the script re-directs you back to itself, and that
                            >> redirection stops your browser wanting to resubmit the data when
                            >> refreshed. The session is then checked, and if the data is present, a
                            >> thank-you note is displayed. If not, it then shows the initial form.
                            >>
                            >> dave
                            >>[/color][/color]

                            His code should work fine as long as you don't have *anything* before
                            the first line - including blank lines, spaces and DOCTYPE statements.

                            You should not need ob_start().

                            --
                            =============== ===
                            Remove the "x" from my email address
                            Jerry Stuckle
                            JDS Computer Training Corp.
                            jstucklex@attgl obal.net
                            =============== ===

                            Comment

                            • Jasen Betts

                              #15
                              Re: Clearing POST DATA for Browser Refresh

                              On 2006-02-14, tmax <tmax@comcast.n et> wrote:[color=blue]
                              > PHP Pros:
                              >
                              > I have a simple html form that submits data to a php script, which
                              > processes it, and then redisplays the same page, but with a "thank you"
                              > message in place of the html form. This is all working fine. However,
                              > when refresh the browser, I get the following message displayed:
                              >
                              > "The page you are trying to view contains POSTDATA. If you resend the
                              > data, any action the form carried out (such as as search or online
                              > purchse) will be repeated. To resend the data, click OK. Otherwise,
                              > click Cancel."
                              >
                              > Obviously I don't want my users to resend the data to me.
                              >
                              > What do I need to do code-wise so that when the browser is refreshed,
                              > the page is reloaded without this message being displayed.
                              >
                              > Thanks in advance.[/color]

                              I think this will work.


                              when they post the form issue a redirect


                              (use a fixed pitch font or the following won't make much sense)

                              user posts automatic user navigates
                              form -----> redirect ------> thankyou --------> further
                              page page

                              when the user goes back from the "further" page they'll hit the
                              thankyou page (which was got by the redirect so no data to post)
                              (back again gets them to the form.)


                              Bye.
                              Jasen

                              Comment

                              Working...