PHP Redirect after a Form Submission

In this chapter you will learn:

  1. How to redirect after form submission
  2. Example - form submission redirections

Description

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  ja v a  2 s  .com
    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 
    } 
    ?> 

Next chapter...

What you will learn in the next chapter:

  1. Description for PHP array() function
  2. Syntax for numeric indexed arrays
  3. Syntax for associative arrays
  4. Example for array() function
  5. Example - Creates an associative array named $age
  6. Example - loop through PHP array
  7. Example - loop associative array
  8. Example - multidimensional array
Home » PHP Tutorial » PHP Form
PHP HTML Forms
PHP GET and POST
PHP Form Elements
PHP Form TextField
PHP Form Password Field
PHP Form Textarea
PHP Form CheckBox
PHP Form Select
PHP Form RadioButton
PHP Form Submit
PHP Form Reset
PHP Form Push Button
PHP Form Image
PHP Form Hidden Field
PHP Split Form Across Pages
PHP Form Validation
PHP Form File Upload
PHP Form Demo
PHP Form Empty Fields
PHP Form Multi-Value Fields
PHP Redirect after a Form Submission