How to redirect to another page in PHP?

Realize: How to divert to another page in PHP? This article contains total instructional exercise on redirection in PHP, which you can use to divert starting with one page then onto the next.

Redirection is an essential piece of the present-day site. On the off chance that something occurs on your site like a client presented a remark or signed in, he ought to be diverted to thank you page or client profile page separately.

PHP gives a straightforward and clean approach to divert your guests to another page that can be either relative or can be cross space.

Here is a straightforward redirection content that will divert to thank you page if the remark is submitted effectively. We will utilize the header(‘location: thankyou.html’)function to divert to the thank you page. The area is the parameter alongside the way to record in the header() function.

PHP code with HTML to divert 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>

This is a basic PHP content, so we put a structure that information sources a name and remark. In the PHP content, we check if the server demand technique is posted since we don’t need this code to execute if the client hasn’t presented the structure through POST strategy, which is just conceivable on the off chance that he present the structure.

Next, we store the values got in $comment and $name variables. At that point we check on the off chance that they are not void, that is they have some value, at that point, we divert the guest to thankyou.html page.

<!DOCTYPE html>
<html>
<head>
    <title>Page Title</title>
</head>
<body>

    <p>Thank you!</p>

</body>
</html>

So this is the means by which you divert to another page utilizing header() function in PHP.

The thank-you page contains a basic thank you message in Html record since it is only for show.

Leave a Comment

error: Alert: Content is protected!!