Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

PHP Build a Simple PHP Application Adding a Contact Form Working with Get Variables

Omahr B. Carpinteyro
seal-mask
.a{fill-rule:evenodd;}techdegree
Omahr B. Carpinteyro
Front End Web Development Techdegree Student 4,684 Points

I 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&rsquo;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&rsquo;ll be in touch shortly</p>
            <?php } else { ?>

2 Answers

Hey 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"){ ?>
Omahr B. Carpinteyro
seal-mask
.a{fill-rule:evenodd;}techdegree
Omahr B. Carpinteyro
Front End Web Development Techdegree Student 4,684 Points

Hi, 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!

Well, I'm just glad I could help out and provide a good answer for you :) Happy Coding, Omahr!

Ryan Field
PLUS
Ryan Field
Courses Plus Student 21,240 Points

Hi, 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
seal-mask
.a{fill-rule:evenodd;}techdegree
Omahr B. Carpinteyro
Front End Web Development Techdegree Student 4,684 Points

Hi, 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&rsquo;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>