PHP Build a Basic PHP Website (2018) Adding a Basic Form Utilizing Object Properties and Methods

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 89,578 Points

Message sends even though there is an error

Okay so I did make another post yesterday, but deleted it just now intending to post again to see if this. But then... guilty... I found what the original problem was. I slightly misspelled variable that meant the condition statement wasn't quite picking up the intended data.

So now what's confusing me is this.

http://www.jonniegrieve.co.uk/jg-lab.co.uk/treehouse/sites/media-library/suggest.php

If I fill in the form only this time use an invalid email, the error message will appear at the top but the email still sends, even though I've set the exit command. See below.

<?php 



    if(!$mail->send()) {
        echo 'Message could not be sent. ';
        echo 'Mailer Error: ' . $mail->ErrorInfo;
        exit;
    }
        header("location:suggest.php?status=thanks");
}

$pageTitle = "Suggest a Media Item";
$section="suggest";

include("inc/header.php"); ?>

        <div class="section page">

            <div class="wrapper">

                <h1>Suggest a Media Item</h1>

                <?php if(isset($_GET["status"]) && $_GET["status"] == "thanks") {
                    echo "<p>Thanks for the email,  I&rsquol;ll check out your suggestion shortly!</p>";
                } else { ?>

                <p>Not seeing what you want in our catalog? Let us know!</p>


                <form method="post" action="suggest.php">

                    <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="text" id="email" name="email" />
                            </td>
                        </tr>
                        <tr>
                            <th>
                                <label for="suggest">Suggestion:</label>

                            </th>
                            <td>
                                <textarea name="suggest" id="suggest" />Send your suggestions here!</textarea>
                            </td>                           
                        </tr>
                        <tr style="display: none">
                            <th>
                                <label for="address">Address:</label>

                            </th>
                            <td>
                                <input type="text" name="address" id="address" />
                                <p>Please leave this field blank. You can safely ignore it.</p>
                            </td>   

                        </tr>
                    </table>

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

                </form>
                <?php  } ?>
            </div>

        </div>

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

Any ideas? :-)