PHP Tutorial - PHP Redirect after a Form Submission






Redirection is done by outputting a Location using PHP using the header() function.

Here's how to redirect to a page called thanks.html:

header( "Location: thanks.html" );

Don't output any content to the browser via echo() or print(), or by including HTML markup outside the <?php ... ?> tags before calling header().

Example

Here's a quick example of a form handler script that redirects to a thank - you page:


<?php //from   ww  w  .j  a va2  s .c om
    if ( isset( $_POST["submitButton"] ) ) { 
      // (deal with the submitted fields here) 
      header( "Location: thanks.html" ); 
      exit; 
    } else { 
      displayForm(); 
    } 
    function displayForm() { 
    ?> 
    <!DOCTYPE html5> 
    <html> 
      <body> 
        <form action="index.php" method="post"> 
            <label for="firstName">First name</label> 
            <input type="text" name="firstName" id="firstName" value="" /> 
            <label for="lastName">Last name</label> 
            <input type="text" name="lastName" id="lastName" value="" /> 
            <input type="submit" name="submitButton" id="submitButton" value= "Send Details" /> 
        </form> 
      </body> 
    </html> 
    <?php 
    } 
    ?>