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

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,016 Points

redirect not working on email form submission.

Remember when I asked about form submission handling a couple of weeks ago? Well, I'm still into this in a big way and I'm putting together so many examples of this online to try and understand.

I have a form I've put together based on this example I created from "The Art Of The Web" https://www.the-art-of-web.com/php/form-handler/

It's almost there. It's functioning fine on localhost and redirects to the thank you page and displays error messages in PHP when done. If the form is okay it submits and sends to my email address.

But it's not doing this when I put it online. https://projects.jonniegrieve.co.uk/form_project/form_handling/

I don't think it's an error to do with PHP versions. I've looked over the code so many times. I'm at a loss as to why the code should behave so differently.

(I will be adding JavaScript later on)

<?php 
    require "header.php";

    if($_POST && isset($_POST['submit'], $_POST['first_name'], $_POST['last_name'], $_POST['e_mail'], $_POST['enquiry'])) {

        //get superglobals
        $name = $_POST["first_name"];
        $last_name = $_POST["last_name"];
        $email = $_POST["e_mail"];
        $message = $_POST["enquiry"];

        $subject = "Title: form sent";

        //capture and report validation errors. 
        if(!$name) {
            $errorMsg = "Please enter your name";
        } elseif (!$last_name){
            $errorMsg = "Second name";
        } elseif(!$email || !preg_match("/^\S+@\S+$/", $email)) {
            $errorMsg = "Please enter a valid email address";
        } elseif(!$message) {
            $errorMsg = "Please enter the details of your enquiry.";
        } else {

            //send email and redirect to confirmation page.
            $to = "/*omitting my email address*/";
            if(!$subject) $subject = "default title";

            //Write email message.
            $response = "Thanks "
             . $name 
             . ". This is the response message" 
             . $message;

            //Email headers!
            $headers = "From: Website" . "\r\n";

            //send email and redirect to confirmation page.
            mail($to, $subject, $response, $message, $headers);

            //redirect to this URL
            header("Location: thank-you.php");

            // exit;
        }
    }

?>      

            <h2>Email Form Handler</h2>

            <!-- Use Main Form -->            

            <form name="main-form" method="post" action="<?php echo $_SERVER["PHP_SELF"]; ?>" id="main-form" class="form">
                <?php require "../assets/forms/form_handling.php"; ?>
            </form>

<?php require "footer.php"  ?>
form_handling.php
            <label for="first_name">First Name:</label>
            <input type="text" name="first_name" id="first_name" title="Enter your first name" placeholder="Enter your name" />
            <?php
            if(isset($errorMsg) && $errorMsg) {
                echo "<p style=\"color: red;\">*",htmlspecialchars($errorMsg),"</p>\n\n";
            }
            ?>

            <p></p>
            <label for="last_name">Last Name:</label>
            <input type="text" name="last_name" id="last_name" title="Enter your last name" placeholder="Enter your last name" />
            <?php
            if(isset($errorMsg) && $errorMsg) {
                echo "<p style=\"color: red;\">*",htmlspecialchars($errorMsg),"</p>\n\n";
            }
            ?>

            <p></p>
            <label for="e_mail">Email:</label>
            <input type="text" name="e_mail" id="e_mail" title="Enter your email" placeholder="Enter your email address" />
            <?php
            if(isset($errorMsg) && $errorMsg) {
                echo "<p style=\"color: red;\">*",htmlspecialchars($errorMsg),"</p>\n\n";
            }
            ?>

            <p></p>
            <label for="enquiry">Enquiry:</label>
            <textarea name="enquiry" id="enquiry" rows="10" cols="40" title="Enter your enquiry"placeholder="Enter your enquiry"></textarea>
            <?php
            if(isset($errorMsg) && $errorMsg) {
                echo "<p style=\"color: red;\">*",htmlspecialchars($errorMsg),"</p>\n\n";
            }
            ?>

            <p></p>                
            <input type="submit" class="form_submit" value="Send!" name="submit" />
            <input type="reset" class="form_reset" value="Reset" name="reset" />

2 Answers

Hi. you can try to ask the question in reddit or summon a experienced developer such as steven parker. I just started to learn PHP in treehouse, how is the courses so far? did u enjoy?

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,016 Points

Thanks for your suggestion, Dot.

And true to form I think I've found the answer. It looks like I fed an invalid argument, an extra argument into the mail() function. So I removed the $message variable. I'm now getting emails returned but only if there are no validation errors. Breakthrough. :)

Although I still don't know why redirect isn't functioning I think it's something to do with the behaviour with the PHP_SELF directive. It's somehow doing that before it knows about doing a HTTP header redirect.

I think you'll like the PHP courses so far. There is a tutorial on Treehouse one of the "build a website" courses that walks through using a web form that sends an email using the PHPMailer package. I'm building up to that, I think but I just want to start with the basics and build examples of more rudimentary forms and their validation solutions.

Thanks for the answer!. glad u made some progress those are the moments where u really enjoy it in my opinion. can u answer in my latest discussion about the php tracks ?