五表单页面提交后跳转至感谢页,能否根据来源修改<h1>标签?
Hey Jim, great question! Let's break down how to show custom <h1> headings on your thank-you page depending on which form was submitted. Here are two solid approaches you can implement right away:
Approach 1: Pass Form Type via URL Parameter
This is the simplest method, perfect if you don't mind the form type appearing in the URL.
Step 1: Add a hidden field to each form
For each of your five forms, add a hidden input that identifies the form type. For example:<!-- Comment Form --> <form action="mail-handler.php" method="POST"> <input type="hidden" name="form_type" value="comment"> <!-- Rest of your comment form fields --> </form> <!-- Contact Form --> <form action="mail-handler.php" method="POST"> <input type="hidden" name="form_type" value="contact"> <!-- Rest of your contact form fields --> </form>Use unique
valuevalues for each form (e.g.,feedback,support,review).Step 2: Modify your mail handler to pass the parameter
In your PHP mail processing file, after sending the email, redirect to the thank-you page with theform_typeparameter:// Process the email (your existing code here) // ... // Get the form type from the submitted data $form_type = $_POST['form_type']; // Redirect to thank-you page with the form type parameter header("Location: thank-you.php?form=$form_type"); exit; // Always exit after a header redirect to stop further code executionStep 3: Display the custom heading on the thank-you page
Inthank-you.php, read the URL parameter and output the matching heading:<?php // Set a default heading in case the parameter is missing $heading = "感谢您的提交"; // Check if the form type parameter exists if (isset($_GET['form'])) { $form_type = $_GET['form']; // Switch heading based on form type switch ($form_type) { case 'comment': $heading = "感谢您的评论"; break; case 'contact': $heading = "感谢您的联系"; break; case 'feedback': $heading = "感谢您的反馈"; break; // Add cases for your other three forms here default: $heading = "感谢您的提交"; } } ?> <!-- Output the heading in your HTML --> <h1><?php echo $heading; ?></h1> <!-- Rest of your thank-you page content -->
Approach 2: Use Session Storage (No URL Parameter)
If you prefer not to expose the form type in the URL, use PHP sessions to store the form type temporarily.
Step 1: Add the hidden form field (same as Approach 1)
Include theform_typehidden input in each form just like before.Step 2: Store form type in session in the mail handler
Start a session at the top of your mail handler, save the form type, then redirect:session_start(); // Must be called before any HTML output // Process the email (your existing code here) // ... // Store form type in session $_SESSION['form_type'] = $_POST['form_type']; // Redirect to thank-you page header("Location: thank-you.php"); exit;Step 3: Retrieve session data in the thank-you page
Inthank-you.php, start the session, read the stored form type, output the heading, then clear the session value (so it doesn't show up if the user refreshes the page):<?php session_start(); // Again, call before any HTML output $heading = "感谢您的提交"; if (isset($_SESSION['form_type'])) { $form_type = $_SESSION['form_type']; switch ($form_type) { case 'comment': $heading = "感谢您的评论"; break; case 'contact': $heading = "感谢您的联系"; break; // Add other form cases here default: $heading = "感谢您的提交"; } // Clear the session value to avoid reusing it on refresh unset($_SESSION['form_type']); } ?> <h1><?php echo $heading; ?></h1> <!-- Rest of your thank-you page content -->
Quick Notes
- Make sure all forms use the same
namefor the hidden field (form_type) so your PHP code can read it consistently. - Always use
exitordieimmediately after aheader("Location: ...")redirect to prevent leftover code from running. - For session-based approach, ensure no HTML (including whitespace) is output before
session_start(), otherwise you'll get an error.
内容的提问来源于stack exchange,提问作者Jim of Redlands

