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

My form will not submit or re-direct

I have typed the code exactly as the instructor has it but my form refuses to move on to the thank you message or even re-direct to a new page after inputting the information. I'm thinking that there is an extra space or something in the code but I can't seem to find it please help. Below is a copy of my code that also has the amended header position that other students suggested:

*CODE COPY*

<?php

header( "refresh:5;url=contact-thanks.php" ); echo 'You\'ll be redirected in about 5 secs. If not, click <a href="index.php">here</a>.';

$name = $_POST["name"]; $email = $_POST["email"]; $message = $_POST["message"]; $email_body =""; $email_body = $email_body . "Name: " . $name . "\n"; $email_body = $email_body . "Email: " . $email . "\n"; $email_body = $email_body . "Message: " . $message;

//TODO: Send Email

$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"); ?>

1 Answer

daviem
daviem
21,118 Points

Hi Phillip,

To get it to redirect to another page - which is how Randy shows it first, try this: (sorry its so longwinded it makes no sense without posting the whole code.. )

Can you go to the html form in contact.php and make sure that you have something like the following:

making sure the method = post and there is no action attribute set (so that it posts the name, email and message entered by the user ) back to this same file (contact.php)

            <form method="post"> <!-- make sure it's set to post!!  -->

                <table>

                    <tr>
                        <th><label for="name">Name:</label></th>
                        <td>
                            <input type="text" id="name" name="name">
                        </td>
                    </tr>
                    <tr>
                        <th><label for="email">Email:</label></th>
                        <td>
                            <input type="email" id="email" name="email">
                        </td>
                    </tr>
                    <tr>
                        <th><label for="message">Message:</th>
                        <td>
                            <textarea name="message" id="message"></textarea>
                        </td>                           
                    </tr>

                </table>

                <input type="submit" value="send">

            </form>

so now when the send button is pressed if you have set the name attributes on each input element, the name, email, message is posted to server and to the same contact.php file and we can then pull them out using $_POST[].

To retrieve the posted values, scroll right up to the top of the same file (contact.php) and start a php block as follows:

if($_SERVER["REQUEST_METHOD"] == "POST"){ // if the form method is post .. ie if the send button is pressed, then run the below and load the posted variables into php ones $name = $_POST["name"]; $email = $_POST["email"]; $message = $_POST["message"];

// then redirect the user to a confirmation page as below: header("Location:contact-thanks.php"); exit; }


NB. Note for this header to work you must have a contact-thanks.php file IN THE SAME directory as your contact.php file

Now for the confirmation page!

// contact-thanks.php (in same directory as contact.php)


<?php 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"); ?>

Hope this helps explain the re-direct!

good luck bud.

Thanks so much for your help, that fixed the problem perfectly. I couldn't but laugh at myself when I realized later on in the video that the instructor was actually trying to show us the wrong way to write the code and later on goes on to show us the correct way to do it. someone should mention that in a side note or something because I had already asked you for help before I even noticed what was going on