How to redirect to another page in PHP

0 votes

How to redirect to another page in PHP?

I want to know how to redirect users to another page in PHP, either after form submission or certain actions. I'm looking for the correct method to set up a redirect, including best practices for ensuring it works smoothly without issues. What’s the proper way to redirect to a different page in PHP?

Oct 28, 2024 in Web Development by Nidhi
• 8,920 points
198 views

1 answer to this question.

0 votes

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.


 

answered Oct 28, 2024 by kavya

Related Questions In Web Development

0 votes
1 answer

How to track with Google Analytics on a redirection page with PHP?

Hello @kartik, Since the page that is sending ...READ MORE

answered Jul 7, 2020 in Web Development by Niroj
• 82,840 points
1,988 views
0 votes
1 answer

How do I redirect to another webpage?

To redirect from an HTML page, use the ...READ MORE

answered Jun 23, 2022 in Web Development by rajatha
• 7,680 points
544 views
0 votes
1 answer

How to Return JSON from PHP to JavaScript?

Hello @kartik, Php has an inbuilt JSON Serialising ...READ MORE

answered Jul 6, 2020 in Web Development by Niroj
• 82,840 points
1,724 views
+1 vote
2 answers

Scp Php files into server using gradle

Tru something like this: plugins { id ...READ MORE

answered Oct 11, 2018 in DevOps & Agile by lina
• 8,220 points
1,731 views
0 votes
1 answer

How do I create folder under an Amazon S3 bucket through PHP API?

Of Course, it is possible to create ...READ MORE

answered Apr 24, 2018 in AWS by anonymous
11,576 views
0 votes
1 answer

Failure uploading Image on AmazonS3 with PHP SDK

Try this, I took it out from ...READ MORE

answered May 4, 2018 in AWS by Cloud gunner
• 4,670 points
4,669 views
0 votes
1 answer

Trying to call AWS API via PHP

Try using AWS SDK for PHP, Link ...READ MORE

answered Jun 6, 2018 in AWS by Cloud gunner
• 4,670 points
1,847 views
0 votes
1 answer

How to implement a debounce time in keyup event in Angular 6

To implement a debounce time in keyup ...READ MORE

answered Oct 25, 2024 in Web Development by kavya
150 views
0 votes
1 answer

How to run an Angular project in Visual Studio Code?

There are some prerequisites to run an ...READ MORE

answered Oct 28, 2024 in Web Development by kavya
275 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP