title: Display alert box upon form submission
body: <p>So I have these two pages: <code>pageOne.php</code> and <code>pageTwo.php</code>.The form is in <code>pageOne.php</code>:</p>
  
  <pre><code>&lt;form method="post" action="pageTwo.php"&gt; .... &lt;/form&gt;
  </code></pre>
  
  <p>and doing all the data collection-validation-insertion and sending out mails in <code>pageTwo.php</code> (the reason i'm doing everything in two separate pages is to avoid the data re-submission upon page refresh...this was the easiest way for me to handle the issue). So far everything is working perfectly.</p>
  
  <p>Now, I want to display a success/failure message using alert box after the form submission and tried few things w/o any luck. E.g. when I tried <a href="https://stackoverflow.com/questions/13328791/php-html-alert-box-when-the-registration-is-successful">THIS</a> solution on <code>pageTwo.php</code>, no pop up box shows up and I think that's because I have this <code>header</code> on top of that page </p>
  
  <pre><code>&lt;?php header("Location: http://TestPages.com/pageOne.php");  ?&gt;
  
  &lt;?php
  if( $_POST ) {
       //collect the data
       //insert the data into DB
       //send out the mails IFF the data insertion works
  
       echo "&lt;script type='text/javascript'&gt;alert('It worked!')&lt;/script&gt;";
  }else
       echo "&lt;script type='text/javascript'&gt;alert('Did NOT work')&lt;/script&gt;";
  ?&gt;
  </code></pre>
  
  <p>And when tried this <a href="https://stackoverflow.com/questions/16250596/successful-fail-message-pop-up-box-after-submit"> second solution</a> in <code>pageOne.php</code>, I get the alert box popping up every time i refresh the page and get the failure message even though the data had been inserted into database and mails were sent out. <code>pageOne.php</code>: </p>
  
  <pre><code>&lt;html&gt;
  &lt;body&gt;
     &lt;?php
     if( $GLOBALS["posted"])  //if($posted) 
        echo "&lt;script type='text/javascript'&gt;alert('It worked!')&lt;/script&gt;";
     else
        echo "&lt;script type='text/javascript'&gt;alert('Did NOT work')&lt;/script&gt;";
     ?&gt;
     &lt;form method="post" action="pageTwo.php"&gt; .... &lt;/form&gt;
  &lt;/body&gt;
  </code></pre>
  
  <p>and in <code>pageTwo.php</code>:</p>
  
  <pre><code>&lt;?php header("Location: http://TestPages.com/pageOne.php");  ?&gt;
  
  &lt;?php
  $posted = false;
  if( $_POST ) {
       $posted = true;
       //collect the data
       //insert the data into DB
       //send out the mails IFF the data insertion works
  }    ?&gt;
  </code></pre>
  
  <p>Why isn't this simple thing working :( ? is there any easy way to fix it? Thank you!!</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>So I have made some changes according to <em>drrcknlsn</em>'s sugession and this is what I have so far....<code>pageOne.php</code>: </p>
  
  <pre><code> &lt;?php
     session_start();
  
     if (isset($_SESSION['posted']) &amp;&amp; $_SESSION['posted']) {
        unset($_SESSION['posted']);
        // the form was posted - do something here
    echo "&lt;script type='text/javascript'&gt;alert('It worked!')&lt;/script&gt;";
     } else
        echo "&lt;script type='text/javascript'&gt;alert('Did NOT work')&lt;/script&gt;";
   ?&gt;
  &lt;html&gt; &lt;body&gt;
     &lt;form method="post" action="pageTwo.php"&gt; .... &lt;/form&gt;
  &lt;/body&gt; &lt;/html&gt;
  </code></pre>
  
  <p>and <code>pageTwo.php</code>:</p>
  
  <pre><code>&lt;?php
  session_start();
  
  if ($_SERVER['REQUEST_METHOD'] === 'POST') {
       $_SESSION['posted'] = true;
  
       //collect the data
       //insert the data into DB
       //send out the mails IFF the data insertion works
  
       header('Location: http://TestPages.com/pageOne.php');
       exit;
  } ?&gt;
  </code></pre>
  
  <p>With these changes now the page's redirection and success message is working, but i get the failure msg every time i open/refresh the page (i know that's because the session key is not set yet)...<strong>how can i avoid that?</strong> Thanks again!!</p>