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>