PHP provides a simple and clean way to redirect your visitors to another page that can be either relative or can be cross domain.
In the example below , it redirect to thank you page if the comment is submitted successfully. We will use the header('location: thankyou.html')function to redirect to the thank you page. The location is the parameter along with the path to file in the header() function.
PHP code to redirect to another page
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$comment = $_POST['comment'];
$name = $_POST['name'];
if($comment && $name) {
header('location: thankyou.html');
}
}
?>
<form action="comment.php" method="POST">
<input type="text" name="Name">
<textarea name="comment"></textarea>
<input type="submit" value="submit">
</form>
</body>
</html>
Explanation
This is a simple PHP script, so we put a form that inputs a name and comment. In the PHP script we check if the server request method is post because we don’t want this code to execute if user hasn’t submitted the form through POST method, which is only possible if he submit the form.
Next, we store the values received in $comment and $name variables. Then we check if they are not empty, that is they have some value, then we redirect the visitor to thankyou.html page.