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 trialLeigh Maher
21,830 PointsHow do I show a summary of the fields filled out?
I want to show the user a summary of the fields that they filled out on the thank you page (?status=thanks). Before when we had the thanks page as a separate php file, I was able to use a $_SESSION to carry the $email_body variable across to that page and then display it to the user.
With the single page solution, I can't get the details to show just using the $email_body variable.
If I'm using the same page shouldn't I be able to use that variable?
Or does php not carry variables if the url changes at all i.e.
from: /suggest.php to: /suggest.php?status=thanks
Do I need to use some kind of session variable? I tried but couldn't get it to work.
4 Answers
Benjamin Larson
34,055 PointsLewis, I don't believe if statements in PHP have any bearing on variable scope in the same way that functions do. I think the issue here is that the redirect is doing a new HTTP GET request, so the $email_body variable will not carry over automatically. You are essentially trying to send a POST request and a GET request simultaneously. For a given request, you can only use one request method, but one option might be to setup the form like the following:
<form name="suggestions" method="post" action="suggest.php?status=thanks">
In this way, you could pass the email_body as post data, while still populating the $_GET['status'] variable, which acts more like a boolean switch as opposed to requiring any specific data.
Benjamin Larson
34,055 PointsI agree with Lewis that to give a better answer would require seeing the code involved, but to at least address the question about the lifetime of variables, in addition to what Lewis said about local scope, it should be noted that variables will not carry over with a new page request, even if it's the same page. This is because HTTP and PHP are stateless, meaning each request (new page or page reload) remembers nothing from the one before it. Any such abstraction (such as sessions) only mimics the behavior of remembering state, but each HTTP request is distinct and any data that needs to get passed around, needs to be passed on each request.
You should be able to accomplish this through the use of sessions, databases, files, or passing the variables back through either a GET or POST request.
Lewis Combey
2,533 Points^ Yeah what he said ;)
Lewis Combey
2,533 PointsWithout being able to see everything this is abit hard however if you are trying to declare a varible on the thankyoupage you need to one make sure that the varible is not just sotred in a function because if it is then outside of the function it will not work.
Secondly you need to determine whether you have ended your php loop for example because if so it will end that script running and therefore you wont be able to declare the variable.
If this does not make sense feel free to message me further.
Leigh Maher
21,830 PointsThanks Lewis and Benjamin. The variable is in an if statement but not a function. I've tried initialising the variable outside the if statement but that didn't work. Here's the original code:
<?php
// If we set method=post in the form
if($_SERVER["REQUEST_METHOD"] == "POST") {
// we take the values from the fields from the suggestion form using post method
$name = $_POST['name'];
$email = $_POST['email'];
$detail = $_POST['details'];
// create a variable to hold all the values from the form
$email_body = '';
$email_body .= "<div>Name: {$name}</div>";
$email_body .= "<div>Email: {$email}</div>";
$email_body .= "<div>Details: {$detail}</div>";
// redirect to the thank you page
header("location:suggest.php?status=thanks");
}
?>
And the part where I'm trying to output the $email_body variable:
<?php
if(isset($_GET['status']) && $_GET['status'] == 'thanks') { ?>
<div class="wrapper">
<h1>Thank you<h1>
<p>Thanks for the email. I'll check out your suggestion</p>
//Trying to call it HERE
<p><?php echo $email_body; ?></p>
</div>
<?php } else { ?>
Note: the form is located on the suggest.php page and the thank you is same page with ?status=thanks
Lewis Combey
2,533 PointsYes because you set the variable in the other if statement you cannot then reference it in another if statement
Leigh Maher
21,830 PointsLeigh Maher
21,830 PointsGreat. Thanks Benjamin. I removed the redirect and changed to action="suggest.php?status=thanks", and it worked : )
Benjamin Larson
34,055 PointsBenjamin Larson
34,055 PointsGlad to hear that fixed it! Though it's surprising even to me, given that I had no idea how to address your question when I read it haha. Most of the time I feel as though I have no idea what I'm doing :)
Leigh Maher
21,830 PointsLeigh Maher
21,830 PointsYou're probably way underestimating yourself. You certainly sound like you know what you're talking about : )