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

How do I have the header redirect to a completely different page?

I am integrating this contact.php file in an angular project to handle the contact form.

I wish to re-direct the page AFTER the form has been submitted to go to the index file ex: /index -or- #index ... etc.

I've tried everything i could think of in the PHP documentation, but I don't know php very well.

Could someone please help me redirect the page to go to #index after the form submits? I think the answer lies somewhere in the isset conditional statement:

<?php if (isset($_GET["status"]) AND $_GET["status"] == "thanks") { ?>

      <?php     header("Location: #index"); ?>

Please Help!

HERE'S MY CODE:

<?php
        if (!$mail->Send()) {

            echo 'Mailer Error: ' . $mail->ErrorInfo;
            exit;
        } 

    header("Location: contact.php?status=thanks");

    exit;

}

?>

<?php 
$pageTitle = "Contact us";
$section = "contact";
 ?>

    <div class="section page">

        <div class="wrapper">

            <h1>Contact</h1>


            <?php if (isset($_GET["status"]) AND $_GET["status"] == "thanks") { ?>

          <?php     header("Location: #index"); ?>

            <?php } else { ?>

                <p>We&rsquo;d love to hear from you! Complete the form to send me an email.</p>



                <form action="contact.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>
                        </tr>  

                    <!-- DUMMY FIELD -->

                        <tr style="display:none;">
                            <th>
                                <label for="address">address</label>
                            </th>

                            <td>
                                <input type="address" id="address">
                                <p>Humans only! Leave this section blank</p>
                            </td>
                        </tr>  

                    </table>
                <input type="submit" value="Send">

                </form>

            <?php } ?>



        </div>

    </div>


</body>

5 Answers

Hi Forgmi,

Have you tried this?

<?php  header("Location: /index.html"); ?>

And if this doesn't work, what does your file structure look like?

By the way, I think you only need the redirect in the top.

Best regards,

Philip

Hi Philip,

Thanks for the suggestion.

I'm not sure how familiar you are with Angular, but I'm using ng-route to manage all the redirections. With this said, I've done this for the ng route relating to contact.php file:

when: /contact.php; templateURL: /html/views/index.html

My hope was to have the contact.php file route BACK to the index after submitting. But this has not worked. This is my first php project and am struggling with the syntax. Thanks in advance!

contact.php index.html css js -> app.js html |->views ||->->navbar.html ||->->contact.html ||->->home.html ||->->services.html ||->->careers.html ||->->footer.html ||->->products.html ||->->about.html ||->->clients.html

Oh, I haven't read that you are using Angular :) I'm not very experienced with Angular, but on Success, try whether this:

$window.location.href = '/index';
$window.location.href;

Or that:

$location.path('/index');

If that doesn't work, please post your Angular code.,

Best regards, Philip

Philip - To clarify, you're suggestion is to place $window.location.href = '/index'; $window.location.href; Or that:

$location.path('/index');

into the php code? Or into the ng-route?

Into the ng-route