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 trialOmahr B. Carpinteyro
Front End Web Development Techdegree Student 4,684 PointsI want to display the input name in the thanks message, but the echo command only works with string, not with variables
Hi!! I want to display the user name in the <p> tag for the thanks message.
This works:
<?php if ( isset($_GET["status"]) AND $_GET["status"] == "thanks"){ ?>
<p>thanks for your email, <?php echo "dani"; ?> ! I’ll be in touch shortly</p>
<?php } else { ?>
This doesn't work, only displays a blank space instead of the name:
<?php if ( isset($_GET["status"]) AND $_GET["status"] == "thanks"){ ?>
<p>thanks for your email, <?php echo $name; ?> ! I’ll be in touch shortly</p>
<?php } else { ?>
2 Answers
Marcus Parsons
15,719 PointsHey Omahr,
It looks like you are setting status
to "gracias" and not "thanks" when you change the header. So, when you're testing to see if status
is equal to "thanks" it's going to fail each time because it's equal to "gracias". You can do a couple different things to get the expected result.
You should either change the header location to have a status of "thanks":
header("Location: contacto.php?status=thanks");
Or change the if-statement to check to see if status is "gracias":
<?php if ( isset($_GET["status"]) AND $_GET["status"] == "gracias"){ ?>
Ryan Field
Courses Plus Student 21,242 PointsHi, Omahr. The way you have your code written, it should work as long as $name
is set to something before that. If it's just returning a single space, then it's likely that $name
contains no value, so you might want to check over your code again.
Omahr B. Carpinteyro
Front End Web Development Techdegree Student 4,684 PointsHi, Ryan! My $name variable is working fine through all the exercise
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST" ) {
header("Location: contacto.php?status=gracias");
$name = $_POST["name"];
$email = $_POST["email"];
$message = $_POST["message"];
$bodymail = "Name: " . $name . "\n" . "Email: " . $email . "\n" . "Message: " . $message;
echo $bodymail;
}
?>
<div>
<?php if ( isset($_GET["status"]) AND $_GET["status"] == "gracias") { ?>
<p>thanks for your email, <?php echo $name; ?> ! I’ll be in touch shortly</p>
<?php } else { ?>
<form method="post" action="contacto.php">
<table>
<tr>
<th><label for="name">Name</label></th>
<td><input type="text" name="name" id="name"></td>
</tr>
<tr>
<th><label for="email">Email</label></th>
<td><input type="text" name="email" id="email"></td>
</tr>
<tr>
<th><label for="message">Message</label></th>
<td><textarea name="message" id="message"></textarea></td>
</tr>
</table>
<input type="submit" value="enviar">
</form>
<?php } ?>
</div>
Omahr B. Carpinteyro
Front End Web Development Techdegree Student 4,684 PointsOmahr B. Carpinteyro
Front End Web Development Techdegree Student 4,684 PointsHi, Marcus! I have two documents, one with variables in spanish and one in english! I cross the code when copying and pasting to this forum... I apoligize for this!! I've updated my code!!
Thanks!
Marcus Parsons
15,719 PointsMarcus Parsons
15,719 PointsWell, I'm just glad I could help out and provide a good answer for you :) Happy Coding, Omahr!