-1

I am trying to make a contact form for my website with PHP. I am also using PHP Mailer to email the message. After pressing the send button on the form the page should redirect to itself, but instead it goes to the correct link but not the correct page. Here is the code:

<?php 

if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = trim($_POST["name"]);
$email = trim($_POST["email"]);
$message = trim($_POST["message"]);

if ($name == "" OR $email == "" OR $message == "") {
    $error_message = "Oops! Make sure you fill out all of the information!";
}

if (!isset($error_message)) {
    foreach( $_POST as $value ){
        if( stripos($value,'Content-Type:') !== FALSE ){
            $error_message = "There was a problem with the information you entered.";
        }
    }
}

if (!isset($error_message) && $_POST["address"] != "") {
    $error_message = "Your form submission has an error.";
}

require_once("inc/phpmailer/class.phpmailer.php");
$mail = new PHPMailer();

if (!isset($error_message) && !$mail->ValidateAddress($email)){
    $error_message = "You must specify a valid email address.";
}

if (!isset($error_message)) {
    $email_body = "";
    $email_body = $email_body . "Name: " . $name . "<br>";
    $email_body = $email_body . "Email: " . $email . "<br>";
    $email_body = $email_body . "Message: " . $message;

    $mail->SetFrom($email, $name);
    $address = "myemail";
    $mail->AddAddress($address, "name");
    $mail->Subject    = "name Contact Form Submission | " . $name;
    $mail->MsgHTML($email_body); 

    if($mail->Send()) {
        header("Location: contact.php?status=thanks");  
        exit;
    } else {
      $error_message = "There was a problem sending the email: " . $mail->ErrorInfo;
    }

}
}

?><?php
$pageTitle = "Contact Us";
$section = "contact";
include('inc/header.php'); ?>

<center>

    <div class="contact-form">

        <h1 id="text">Contact</h1>

        <?php if (isset($_GET["status"]) AND $_GET["status"] == "thanks") { ?>
            <p id="text">Thanks for the email! I&rsquo;ll be in touch shortly!</p>
        <?php } else { ?>

            <?php
                if (!isset($error_message)) {
                    echo '<p id="text">We&rsquo;d love to hear from you! Complete the form to send us an email.</p>';
                } else {
                    echo '<p id="text">' . $error_message . '</p>';
                }
            ?>

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

                <table>
                    <tr>
                        <th>
                            <label for="name">Name</label>
                        </th>
                        <td>
                            <input type="text" name="name" id="name" value="<?php if (isset($name)) { echo htmlspecialchars($name); } ?>">
                        </td>
                    </tr>
                    <tr>
                        <th>
                            <label for="email">Email</label>
                        </th>
                        <td>
                            <input type="text" name="email" id="email" value="<?php if(isset($email)) { echo htmlspecialchars($email); } ?>">
                        </td>
                    </tr>
                    <tr>
                        <th>
                            <label for="message">Message</label>
                        </th>
                        <td>
                            <textarea name="message" id="message"><?php if (isset($message)) { echo htmlspecialchars($message); } ?></textarea>
                        </td>
                    </tr> 
                    <tr style="display: none;">
                        <th>
                            <label for="address">Address</label>
                        </th>
                        <td>
                            <input type="text" name="address" id="address">
                            <p>Visitor: Please leave this field blank.</p>
                        </td>
                    </tr>                   
                </table>
                <input type="submit" value="Send">

            </form>

        <?php } ?>

    </center>

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

A link to this problem is http://pokobrosapps.com/contact.php. Why is this happening?

PoKoBros
  • 701
  • 3
  • 9
  • 25
  • Where is redirecting to? – John Conde Aug 07 '14 at 19:41
  • 1
    Did you turn `error_reporting` on to see if it gives you any info? – Unexpected Pair of Colons Aug 07 '14 at 19:44
  • Sorry I am new to PHP how would I turn error_reporting on @UnexpectedPairofColons? And @John Conde the best way to answer your question would be probably trying the contact form at the link in the question. – PoKoBros Aug 07 '14 at 19:50
  • 1
    @PoKoBros add `error_reporting(E_ALL)` after your opening PHP tag, and see if that gives you anything. – Unexpected Pair of Colons Aug 07 '14 at 19:52
  • I added it and all I got was a blank page after I reloaded the page in MAMP. – PoKoBros Aug 07 '14 at 19:53
  • not likely, but could be a operator precedence (http://php.net/manual/en/language.operators.precedence.php). Try changing `if ($name == "" OR $email == "" OR $message == "") {` to `if ($name == "" || $email == "" || $message == "") {` and `if (isset($_GET["status"]) AND $_GET["status"] == "thanks") {` to `if (isset($_GET["status"]) && $_GET["status"] == "thanks") {` http://stackoverflow.com/questions/2803321/and-vs-as-operator – Sean Aug 07 '14 at 20:14
  • @PoKoBros did you submit the form, also? It probably won't produce any errors until you submit the form. – Unexpected Pair of Colons Aug 07 '14 at 20:17
  • Sean it didn't work. @UnexpectedPairOfColons I cannot submit the form. When I add the code to the page, all I get is a blank white screen. I am running this on a local server with MAMP. – PoKoBros Aug 07 '14 at 20:22
  • Alright now I was able to add the error_reporting line to the page. Where would I be able to view the error log on a local server? – PoKoBros Aug 07 '14 at 20:27

1 Answers1

0

I got the contact form to work! I figured out how to get an error log working with MAMP from here: http://gilbert.pellegrom.me/enable-php-error-reporting-in-mamp/ Then it said that the PHP Mailer file was not where it was supposed to be, so I fixed that and the contact form is now working!

PoKoBros
  • 701
  • 3
  • 9
  • 25