title: How to redirect user to another page using javascript in php?
body: <p>I want to redirect the user to another page only after displaying a success message. The code below redirects to the another page even if the page is refreshed without submitting any data.</p>
  
  <p><em>Code Snippet</em></p>
  
  <pre><code>&lt;?php
  // We will store our status message later
  $message = '';
  
  // Let's check form submitted
  if( isset( $_POST['action'] ) &amp;&amp; $_POST['action'] == 'submit' ) {
  
      $name = $_POST['name']; 
      $email = $_POST['email']; 
      $phone = $_POST['number'];
  
      $message_text = $_POST['address'];
  
  
          if (isset($_POST['action'])) 
          {   
  
              // Validate required fields 
              if( $name == '' || $email == '' || $message_text == '' ){
                  $message = "&lt;p class=\"error\"&gt;Please fill out all required fields.&lt;/p&gt;";  
              }
  
  
              // send email after validation
              else {
  
                  // Email will be sent at this email
                  $mailto = 'nepstarditsolutions@gmail.com';
  
                  // Let's create html email format 
                  // (You can use html in this email format)
  
                  // Email headers
                  $headers = "From: " . strip_tags( $email ) . "\r\n";
                  $headers .= "Reply-To: ". strip_tags( $email ) . "\r\n";
                  $headers .= "MIME-Version: 1.0\r\n";
                  $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
  
                  // Email body
                  $message = "&lt;html&gt;&lt;body&gt;";
                  $message .= "&lt;h3&gt;Contact Info: &lt;/h3&gt;";
                  $message .= "&lt;p&gt;&lt;strong&gt;Name: &lt;/strong&gt; ". strip_tags( $name ) . "&lt;/p&gt;\r\n";
                  $message .= "&lt;p&gt;&lt;strong&gt;Email: &lt;/strong&gt; ". strip_tags( $email ) . "&lt;/p&gt;\r\n";
  
                  $message .= "&lt;p&gt;&lt;strong&gt;Contact Number: &lt;/strong&gt; ". strip_tags( $phone ) . "&lt;/p&gt;\r\n";
                  $message .= "&lt;p&gt;&lt;strong&gt;Alternate Email: &lt;/strong&gt; ". strip_tags( $_POST['altemail'] ) . "&lt;/p&gt;\r\n";
                  $message .= "&lt;p&gt;&lt;strong&gt;Hosting Package: &lt;/strong&gt; ". strip_tags( $_POST['cradio'] ) . "&lt;/p&gt;\r\n";
  
                  $message .= "&lt;p&gt;&lt;strong&gt;Address: &lt;/strong&gt;". strip_tags( $message_text ) . "&lt;/p&gt;\r\n";
                  $message .= "&lt;/body&gt;&lt;/html&gt;";
  
  
  
                  // Email subject
                  $subject = "Contact Info from ".$name;
  
                  // Send email
                  $mail_status = @mail( $mailto, $subject, $message, $headers );
  
                  if( $mail_status ){
                      $message = '&lt;div class="alert alert-success"&gt;Quotation submitted Successfully ! &lt;/div&gt;';   
                  }
                  else {
                      $message = '&lt;div class="alert alert-danger"&gt;Error in sending the Quotation ! &lt;/div&gt;';  
                  }
  
              }
          }   
  }
  ?&gt;
  </code></pre>
  
  <p><strong>I need help in 2 things</strong></p>
  
  <ol>
  <li>Success message should be displayed for 2 seconds (before the redirect).</li>
  <li>How to redirect using JS in php? </li>
  </ol>