PHP Redirect after a Form Submission
In this chapter you will learn:
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:
- Description for PHP array() function
- Syntax for numeric indexed arrays
- Syntax for associative arrays
- Example for array() function
- Example - Creates an associative array named $age
- Example - loop through PHP array
- Example - loop associative array
- Example - multidimensional array
Home » PHP Tutorial » PHP Form