Question:

How can I get this PHP code to redirect visitor to a new page within my site?

by  |  earlier

0 LIKES UnLike

Currently I have this:

if(count($error) == 0) {

end_mail();

confirm_message();

} else {

form();

}

} else {

form();

}

Where confirm_message() is this:

function confirm_message() {

echo "<div id=\"footer\">\r\n";

echo "<h1>Thank you for ordering! \r\n";

echo "</div>\r\n";

}

However, instead of showing this message to the user. I would like to redirect them to a thank you page.

So, I would like to replace confirm_message() with thank_you() where thank_you() will take the user to a new page within my site (i.e. thankyou.html) that will have a thank you message for the user and that will help me identify successful conversions.

 Tags:

   Report

2 ANSWERS


  1. Use the &quot;header()&quot; function.

    header(&#039;Location: http://www.example.com/&#039;);


  2. You have two ways of doing this:

    1. HTTP Header

    2. Javascript

    If you use the HTTP header, you MUST use it before ANY text is sent to the user. The PHP command looks like this:

    header(&#039;location: thankyou.php&#039;);

    If, for some reason, you may have to send text to the browser before this, then simply use Javascript in your code. Do something like this:

    &lt;?php

    echo &#039;&lt;script&gt;&#039;;

    echo &#039;document.location.href=\&#039;thankyou.php\&#039;...

    echo &#039;&lt;/script&gt;&#039;;

    ?&gt;

    Note that there are subtil differences between the two. In the first situation, if you call your HEADER following a form POST, then if the person hits F5 (Refresh), the form will be resubmitted. However, if you use Javascript, hitting F5 will simply refresh the ThankYou.php page. So it might be better to use this method is you want to avoid multiple form submittals.

    Good luck!

Question Stats

Latest activity: earlier.
This question has 2 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.