title: Prevent resubmit form after click "back" button
body: <p>I have 2 pages :<br/><br/>
  <strong>page1.php :</strong><br/>
  - has a form with text box and a "submit" button. Eg : <code>&lt;form name="frm_register" action="page1.php" method="post"&gt;</code><br/><br/>
  - php and mysql code to store the value of textbox to database. Javascript will redirect the page to <code>php2.php</code> after the value is submitted to database. Eg : </p>
  
  <pre><code>$query = "INSERT INTO traceuser (username) VALUES ('{$username}')";
  $result = mysql_query($query, $connection);
  echo '&lt;script language="javascript"&gt;window.location="page2.php";&lt;/script&gt;';
  </code></pre>
  
  <p><strong>page2.php</strong><br/>
  - mysql retrieve the data from database and display on this page.<br/></p>
  
  <p><strong>Problem :</strong> When I press "back" button, the browser will pop up a warning message saying that the form will be resubmit. How to prevent resubmit the form when click "back" button? Is it I need to clear the cache of page1.php? How to do it with php or javascript or ajax?</p>
  
  <p><hr>
  <strong>Update 1 :</strong> Thanks for the answer of replacing javascript <code>window.location="page2.php"</code> to php <code>header('Location: home2.php');</code>. It fix 80% of problem. The rest of 20% problem show below :</p>
  
  <pre><code>    if (strtotime($_SESSION['servertime']) &lt; time()-3){ //10800 = 3 hours 3600 = 1 hour
                  if (($username != "") AND ($username != $_SESSION[username])){
                      $_SESSION['servertime'] = $servertime; 
                      $_SESSION['username'] = $username;
                      $query = "INSERT INTO traceuser (username) VALUES ('{$username}')";
                      $result = mysql_query($query20, $connection);
                      header('Location: page2.php');
                      exit;
                  } else {
                      echo "same name"; //problem here
                  }
              }else{
                  echo "submit multiple data too fast"; //problem here too.
              }
     }
  </code></pre>
  
  <p>The problem happen when do the following steps :<br/>
  1) User submit data successfully, jump to page2.php view records.<br/>
  2) User click "back" button, jump back to page1.php.<br/>
  3) User submit data fail, stay on page1.php. (because too fast or same name)<br/>
  4) User submit data successful, jump to page2.php view records.<br/>
  5) User click "back" button, but browser shows warning message "form will be resubmited".<br/></p>
  
  <p>The problem is because of Step 3. Step 3 didn't run <code>header('Location: page2.php');</code>, didn't jump to page2.php. So it cause Step 5 show the warning message. How to fix this problem?
  <hr>
  <strong>Update 2 :</strong> I have figured out the solution to fix the 20% problem, it works perfectly. I use <code>session['error123']</code> to decide whether or not want to display the error message "same name". I kill <code>session['error123']</code> if success submit data to database or if success jump to page2.php. I also use <code>header('Location: page1.php');</code> to redirect to own page (same page) to make the page forget about form submission previously. Example of codes :</p>
  
  <pre><code>if ($_SESSION['error123'] == "toofast"){
      echo $_SESSION['error123'] ;
  }elseif ($_SESSION['error123'] == "samename"){
      echo $_SESSION['error123'] ;
  }
  
  if (strtotime($_SESSION['servertime']) &lt; time()-3){ //10800 = 3 hours 3600 = 1 hour
                  if (($username != "") AND ($username != $_SESSION['username'])){
                      $_SESSION['username'] = $username;
                      $query = "INSERT INTO traceuser (username) VALUES ('{$username}')";
                      $result = mysql_query($query20, $connection);
                      $_SESSION['error123'] = "aa";
                      header('Location: http://localhost/plekz/page2.php');
                      exit;
                  } else {
                      $_SESSION['error123'] = "samename";
                      header('Location: http://localhost/plekz/page1.php');
                      exit;
                  }
              }else{
                  $_SESSION['error123'] = "toofast";
                  header('Location: http://localhost/plekz/page1.php');
                      exit;
              }
          }
      }
  </code></pre>
  
  <p>Note : You need to buffer the output by <code>&lt;?php ob_start();?&gt;</code> because $_SESSION cannot put before header(). Buffer will stop all output including session, let header() send the output first.</p>