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 Build a Simple PHP Application Adding a Contact Form Redirecting After a Form Submission

Redirecting After a Form Submission: with Variables

hey,

I`m fooling around with the contact-thanks.php from the tutorial. Trying to echo the "name" and "email adress" from the contact page in my contact-thank.php page. But have no idea.

The result is: It echos the ""thank you for contacting me" but without the name fromthe post variable.

Right now it looks like this:

-------------------------CONTACT.PHP--------------------------

<?php $title = "Contact Me"; $section = "contact"; include("inc/header.php"); ?> <div class="section page"> <h1>Contact</h1>

    <form action="process.php" method="post">
        <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>
        </table>
        <input type="submit" value="Send">

    </form>
</div>

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

------------------------------PROCESS.PHP-----------------------------

<?php $name = $_POST["name"]; $email = $_POST["email"]; $message = $_POST["message"];

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

?>

------------------------CONTACT-THANKS.PHP------------------------------

<?php $title = "Thanks for contacting us"; $section = "contact"; include("inc/header.php");

$name = $_POST["name"]; $email = $_POST["email"]; $message = $_POST["message"]; ?>

<div class="section page">

    <div class="wrapper" style="text-align: center">
        <h1>WooooHoooooooo</h1>
        <?php 
        echo "Thank you " . $name . " for contacting me" ."<br>";  
        echo "I will reply to your email adress " . $email . " as soon as possible";
        ?>
    </div>

</div>

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

1 Answer

Richard Duncan
Richard Duncan
5,568 Points

The easiest way to achieve this would be to add your thanks to the process.php file.

The reason being you are not currently passing name and email to the contact-thanks.php file and hence why nothing is contained in those variables.

The form posts the variables to the process script but not to the contact one. You could in the process script create a php session and store the values in the session, you could then initiate the session on the contact page and retrieve the values that way.

Another similar way would be to store the values in cookies but all this is overkill in my opinion. If in the real world you combined the two that would work just fine.