Question

I want to know how can I return to the previous page...

I am on the searched result page where many results are present. For each result, there's an option such as "EMAIL ME".

When a user clicks on it, a thickbox form is opened where a user is required to provide his name & email id. When a user clicks on "send to submit the form," a entry in our database is made and a email is sent to the user via his/her email id.

The problem is, after submitting the form, I want to redirect the user to a searched result page where the user has the option to send an email back.

Was it helpful?

Solution

You could use Ajax for the form submission and then just close the Thickbox. If Ajax is not an option, you could also redirect the user to the previous page by using the referrer URL.

header("Location: ".$_SERVER["HTTP_REFERER"]);

Update:

Since some browsers, sometimes, don't provide a referrer URL, the above would not work. A work around is to have the form in the Thickbox provide the URL of the search page, which could then be used to redirect.

In the Thickbox form, add a hidden element with the URL. This can be done with JavaScript or PHP.

Then instead of using HTTP_REFERER, use the name of the hidden element.

Form: <input type='hidden' name='searchUrl' value='http://the.search.url' />

PHP: header("Location: ".$_REQUEST["searchUrl"]);

OTHER TIPS

Remember the last location with a session and redirect to it with a Location header.

Add following code to the page you want to return=>

<?php
   session_start();
   $_SESSION["addresser"]="home.php";
?>

Now Add following code to the page you have your form/ or from where you want to return=> First add a hidden field to your form=>

<input type="text" name="valid" hidden="hidden" readonly="readonly" value="true"/>

Now add following php code to the top of this page==>

<?php
session_start();
$location=$_SESSION["addresser"];
if($_SESSION["validate"]="true"){
header("location: $location");}
?>

Also Add following code to the php file where form is submitted....

<?php
$_SESSION["validate"]=$_POST["valid"];
?>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top