Halloween DealsAmazon USHalloween week deals: useful picks before month-endAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run ScanHalloween DealsAmazon USOctober picks are moving - compare before checkoutAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk2 min

How to Redirect to a Page in PHP After a Form Submission

Check the submitted field in the PHP handler, send a Location header for accepted values, and call exit before any output.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

To send visitors to introduction.html when they submit “intro” or “introduction,” check the submitted field in the PHP handler, send a Location header, then call exit. The redirect must happen before PHP outputs any HTML or other content.

Set the form to submit to your PHP handler

The form’s action names the endpoint that receives the submission. Set its method to post if you plan to read the value from $_POST, and make sure the input’s name matches the key your PHP code reads:

As an Amazon Associate I earn from qualifying purchases.

<form action="route.php" method="post">
  <label for="destination">Destination</label>
  <input id="destination" name="destination" type="text">
  <button type="submit">Continue</button>
</form>

Here, the submitted text is available to route.php as $_POST['destination']. The button does not need a name when the handler only needs the text field. The archived SitePoint Forums discussion illustrates why field names and form methods need to agree: examples in the exchange changed names and did not consistently show an explicit method.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Check the value and redirect before output

Put the routing logic at the start of route.php, before a doctype, HTML, whitespace outside PHP tags, or any other output:

<?php
$destination = $_POST['destination'] ?? '';

if ($destination === 'intro' || $destination === 'introduction') {
    header('Location: introduction.html');
    exit;
}

// Continue normal processing for other values.
?>

The two accepted values are compared exactly, so capitalization differences or extra spaces do not match. If you want case-insensitive or whitespace-tolerant input, normalize it deliberately before the comparison rather than silently broadening what is accepted.

PHP’s header() manual says headers must be sent before actual output. A Location header ordinarily produces a 302 redirect unless a 201 or 3xx status has already been set. Calling exit prevents the handler from continuing into code intended for the non-redirect path.

Handle values that do not match

When the submitted value is neither intro nor introduction, the condition is false and PHP continues. Replace the comment with the ordinary form-processing behavior, such as validation and a response or a save operation. Do not assume an unmatched value is safe or valid merely because it did not trigger the redirect.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Keep redirect destinations fixed or allowlisted

For this example, a fixed destination is simplest: the user’s text decides whether to redirect, but it is not inserted into the destination URL. If a form lets users choose among several pages, map accepted values to a fixed allowlist of destinations and redirect only to a matched entry. Avoid passing arbitrary submitted text directly to header('Location: ...'); the forum discussion suggests restricting destinations, but its example should not be treated as a tested production implementation.

Common reasons the redirect fails

  • “Headers already sent”: move the redirect logic before all output, including whitespace before the opening PHP tag.
  • The condition never matches: confirm that the form uses method="post", that the input is named destination, and that the handler reads $_POST['destination'].
  • The form reaches the wrong script: check that its action points to the PHP file containing the routing logic.
  • PHP continues after sending the redirect: place exit immediately after header().

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.