You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

合并快递选择与追踪ID输入生成追踪URL并跳转的PHP代码求助

Fixing Your Multi-Courier Tracking Form

Hey there! Let's break down why your multi-courier tracking code isn't running properly and get it working smoothly:

What's Wrong With the Original Code?

Let's go through the key issues step by step:

  • Redirect is in the wrong spot: Your header('Location: ' . $url); line is placed after the switch block, but in the default case you throw an exception that stops all code execution. Even for valid couriers, this setup isn't reliable.
  • India Post URL missing the tracking ID: You didn't append the user's tracking ID to India Post's URL—so even if the redirect worked, users would land on the tracking page without their ID pre-filled.
  • No stop after redirect: After sending a redirect header, you need to end the script right away with exit() or die(), otherwise extra code might run and mess things up.
  • Exception crashes the page: Throwing an exception for unknown couriers will just show an error to users—way better to give a friendly message instead.

Fixed Working Code

Here's the corrected version that addresses all these issues:

<!DOCTYPE HTML>
<html>
<body>
<form action="#" method="post">
Select Courier : <select name="courier">
<option value="">--Please choose an option--</option>
<option value="professional_courier">Professional Courier</option>
<option value="india_post">India Post</option>
</select>
Trackingid: <input type="text" name="trackingid"><br>
<input type="submit">
</form>
<?php
ini_set('display_startup_errors', 1);
ini_set('display_errors', 1);
error_reporting(-1);

if (!empty($_POST['courier']) && !empty($_POST['trackingid'])) {
    $targetUrl = '';
    switch ($_POST['courier']) {
        case 'professional_courier':
            // Encode the tracking ID to handle special characters
            $targetUrl = "https://www.tpcindia.com/Tracking2014.aspx?id=" . urlencode($_POST["trackingid"]) . "&type=0&service=0";
            break;
        case 'india_post':
            // Update the parameter name here if India Post uses something different (like consignmentid)
            $targetUrl = "https://www.indiapost.gov.in/_layouts/15/dop.portal.tracking/trackconsignment.aspx?tracknum=" . urlencode($_POST["trackingid"]);
            break;
        default:
            // Show a friendly error instead of crashing
            echo "<p style='color: red;'>Oops, we don't recognize that courier. Please pick another option!</p>";
            exit();
    }

    // Only redirect if we have a valid URL
    if (!empty($targetUrl)) {
        header('Location: ' . $targetUrl);
        exit(); // Critical: stop the script here so nothing else runs
    }
}
?>
</body>
</html>

What Did We Change?

  • Fixed redirect placement: Now the redirect runs right after we've set the correct URL for the selected courier, and we only do it if the URL is valid.
  • Added tracking ID to India Post: We appended the user's tracking ID to India Post's URL (just adjust the parameter name like tracknum if India Post uses something else—check their actual tracking URL structure to be sure).
  • Used urlencode(): This makes sure special characters in tracking IDs (like hyphens or letters) are properly formatted for URLs, so the link doesn't break.
  • Replaced exception with a friendly message: Instead of an error page, users get a clear red message telling them to pick a valid courier.
  • Added exit() after redirect: This ensures the script stops immediately after sending the redirect, so no extra code runs unexpectedly.

Quick Bonus Tips

  • Add client-side validation: Use a little JavaScript to make sure users select a courier and enter a tracking ID before they hit submit—this saves them from waiting for a server response to fix mistakes.
  • Test each courier link: Double-check that each courier's URL structure is exactly right—some might use different parameter names (like tracking_id instead of id).

内容的提问来源于stack exchange,提问作者zus

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 09:08:41