0

I'm wondering what is going wrong in this contact form. I'm not sure why, but it constantly defaults to the else and says Something went wrong, please try again later. I can't figure out why, and the error_log isn't showing anything. Is there anything glaringly obvious I'm missing? I'm pretty new to PHP.

PHP

<?php
    $name = $_POST['name'];
    $email = $_POST['email'];
    $phone = $_POST['phone'];
    $message = $_POST['message'];
    $captcha = $_POST['captcha'];
    $from = 'From: HankSmith.com Contact Form'; 
    $to = 'thatbraxjohnsonguy@gmail.com'; 
    $subject = 'HANK SMITH CONTACT FORM';

    $body = "
            From: $name\n 
            E-Mail: $email\n 
            Phone: $phone\n
            Message: $message\n";

    if ($_POST['submit'] and $captcha == 4) {                
        if (mail ($to, $subject, $body, $from)) { 
        echo '<p style="margin-top: 150; text-align:center; font-size: 18px;">Your message has been sent! Click <a href="../index.php">here</a> to return to the website.</p>';
    } else { 
        echo '<p>Something went wrong, try again later!</p>'; 
    }
}
?>

HTML Form

            <form class="contactform" method="post" action="php/contact.php">
                <h3>Name</h3>
                <input class="form inputboxes" type="text" name="name">
                <h3>Email</h3>
                <input class="form inputboxes" type="text" name="email">
                <h3>Phone</h3>
                <input class="form inputboxes" type="text" name="phone">
                <h3>Message</h3>
                <textarea class="form inputboxes" name="message"></textarea>
                <h3 class="captchastyle">Are you real? What is 2 + 2?</h3><input class="captcha captchastyle" type="text" name="captcha" maxlength="1">
                <input class="form submit" name="submit" type="submit" value="Submit">
            </form>
B Johnson
  • 183
  • 1
  • 12

2 Answers2

1

Use isset on your condition:

if (isset($_POST['submit']) && $captcha == 4)

It checks if the field is non empty and the form has been submitted.

And && is better here than and see the reason here: https://stackoverflow.com/a/11861068

Community
  • 1
  • 1
1

When I copied this into my text editor, it threw a parse error because the echo statement in the if (mail)... section didn't have a closing brace. I never use 'and', I usually use '&&', but it actually doesn't seem to matter in this case.

if ($_POST['submit'] && $captcha == 4) {                
        if (mail ($to, $subject, $body, $from)) { 
        echo '<p style="margin-top: 150; text-align:center; font-size: 18px;">Your message has been sent! Click <a href="../index.php">here</a> to return to the website.</p>';
        } // THIS MUST BE HERE
    } else { 
        echo '<p>Something went wrong, try again later!</p>'; 
    }

Also, you probably want an 'else' condition for that if (mail)... statement. If you're seeing a blank page, it will be because you aren't handling what happens if the mail() function returns false:

if (mail ($to, $subject, $body, $from)) { 
  echo '<p style="margin-top: 150; text-align:center; font-size: 18px;">Your message has been sent! Click <a href="../index.php">here</a> to return to the website.</p>';
} else {
  echo '<p>Problem sending mail!';
}

And honestly, the native PHP mail() function sucks! Consider using SwiftMailer or PHPMailer instead.

djs
  • 3,947
  • 3
  • 14
  • 28
  • I forgot a brace?? Figures. Thanks. I'll test that out. – B Johnson Feb 08 '17 at 00:03
  • Okay so it didn't give me an error, just a blank page. Progress! – B Johnson Feb 08 '17 at 00:04
  • Yeah, it's actually not necessary since you only are running one statement in the if conditional. But, since you had an opening brace, the closing brace was necessary. – djs Feb 08 '17 at 00:04
  • You are more than likely getting a blank page because the mail() function returned false, see my edit to my answer for a solution. – djs Feb 08 '17 at 00:07
  • You're exactly right. Now it's throwing `Problem sending mail.` I'll edit the mail function. Thanks for your help! – B Johnson Feb 08 '17 at 00:09