How to return to the previous page after a submit form with php?

How to return to the previous page after a submit form with php?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

How to Return to the Previous Page After a Submit Form with PHP

When you submit a form that processes data on the server, the browser follows a strict rule: it submits the request to the URL specified in the action attribute. In your current setup, the form points directly to contact.php. Once the page loads and runs that script, the browser has no inherent memory of where it came from. It simply displays whatever HTML is generated by the PHP file. If that file only contains an alert box or a blank response, the user sees exactly that, lost in the middle of your application.

To redirect the user back to the form immediately after processing, you must explicitly tell the browser to navigate away. This is handled by outputting an HTTP header before any visible content is sent. The standard way to do this in PHP is using the header() function with a Location directive.

Here is how you modify your contact.php script:

<?php
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];

$to = "pep.g@gmail.com";
$headers = 'From: pep.website@website.com';

if (mail($to, $subject, $message, $headers)) {
    // Successful send
} else {
    // Failed send
}

// Redirect back to the previous URL regardless of success or failure
header("Location: https://laravelcompany.com/your-main-page-url");
exit();
?>

The critical line here is header("Location: ..."). This tells the browser to immediately load a new page. You must place this call before any output, including whitespace or HTML tags, because headers are part of the HTTP response stream and cannot be sent after the body starts. The exit() function follows immediately to stop PHP from executing the rest of the script (like your alert boxes), ensuring no extra data is sent that might confuse the browser's rendering engine.

You have two primary options for the destination URL. The first is to use a relative path, such as header("Location: ../contact_form.html"). This works well if your PHP file and your form are in different directories and you want to maintain a clean folder structure. However, relying on relative paths can be fragile if you move files or change the server configuration later.

The second option is to capture the original request URL using the HTTP_REFERER server variable. This value contains the address of the page that linked to the current script. You can redirect back with:

$referer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '/';
header("Location: $referer");

This approach feels more robust because it dynamically points back to wherever the form was loaded from, whether that is a home page, a specific article, or a dashboard. It handles cases where the form might be embedded in an iframe or accessed directly via a bookmarked URL without breaking the user flow.

There are important trade-offs to consider when implementing this. First, HTTP_REFERER is not always reliable. Some privacy-focused browsers block it by default, and certain server configurations or security headers like Referrer-Policy: no-referrer will prevent PHP from seeing the original URL. If you rely solely on $_SERVER['HTTP_REFERER'], you might accidentally redirect users to an external site or a blank page if the referer is missing. That is why providing a fallback, like the root path /, is good practice.

Second, consider your security implications. Redirecting based on user input (even indirectly via referer) can sometimes open doors to Open Redirect vulnerabilities if the destination isn't strictly validated against your own domain. Always ensure that any redirect target points exclusively to pages under your control.

Finally, remember that mail() is a basic function often criticized for deliverability issues. While fixing the redirect solves the immediate navigation problem, you might also want to explore more robust solutions like Laravel's Mail facade if you are building a larger application, or integrating services like Postmark or SendGrid for better reliability.

Our network