Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Maximiliane Quel
PLUS
Maximiliane Quel
Courses Plus Student 55,489 Points

Retrieving post variables after redirect

Hi,

I was wondering whether anyone knows how to call in a user-submitted variable on the thank you page after a redirect. Is that easy?

If we take the Shirts 4 Mike page example in the building a simple php application project after we redirect, is it possible to redirect in a way so that we can use the name variable in the contact-thanks.php page?

in our contact-process.php we have:

<?php 

$name = $_POST["name"];
$surname = $_POST["surname"];
$email = $_POST["email"];
$message = $_POST["message"];

$email_body = "";
$email_body = $email_body . "Name: " . $name . "\n";
$email_body = $email_body . "Surname: " . $surname . "\n";
$email_body = $email_body . "Email: " . $email . "\n";
$email_body = $email_body . "Message: " . $message;


//TODO: Send Email

header("Location: contact-thanks.php");

?>

in the contact-thanks.php we have

<?php 

$pageTitle = "Contact Mike";
$section = "contact";
include("inc/header.php");

 ?>

 <div class="section page">
     <div class="wrapper">
         <h1>Contact</h1>

         <p>Thanks for the email! I&rsquo;ll be in touch shortly.</p>
     </div>
 </div>

 <?php include("inc/footer.php"); ?>

now is there a way for me to let the contact-thanks.php output:

Thank you $name for the email! I'll be in touch shortly.

Thank you.

2 Answers

The easiest way is to change the header status from <code> header("Location: contact.php?status=thanks"); </code> to '' header("Location: contact.php?status=" . $name); '''

then change the thanks block to ''' if(isset($_GET["status"])){ echo "<p>Thank you " . $_GET["status"] . " for the email! I’ll be in touch shortly.</p>"; } '''

You lose some validation, but you can personalize the thank you message. You could also create a separate file to handle the variable, then delete after the message has been posted, but that would be a wasted process for something this simple.