Okay, so I have a contact form:
<?php
$body = "From: $name
E-Mail: $email
Message:
$message";
if ($_POST['submit']) {
if ($name != '' && $email != '') {
if ($human == '4') {
if (mail ($to, $subject, $body, $from)) {
echo '<p>Your message has been sent!</p>';
} else {
echo '<p>Something went wrong, go back and try again!</p>';
}
} else if ($_POST['submit'] && $human != '4') {
echo '<p>You answered the anti-spam question incorrectly!</p>';
}
} else {
echo '<p>You need to fill in all required fields!</p>';
}
}
?>
And the HTML code to go with it:
<form action="" method="POST">
<!--PHP Code above goes here-->
<div>
<label>Name</label>
<br>
<input name="name" placeholder="Type Here">
</div>
<div>
<label>Email</label>
<br>
<input name="email" type="email" placeholder="Type Here">
</div>
<div>
<label>Message</label>
<br>
<textarea name="message" placeholder="Type Here"></textarea>
</div>
<div>
<label>*What is 2+2? (Spam protection)</label>
<br>
<input name="human" placeholder="Type Here">
</div>
<input id="submit" name="submit" type="submit" value="Submit">
</form>
But even when all of the fields are filled in, it keeps telling me to fill in all the required fields. Thanks in advance.
You need to get the values of all the variables that are submitted via POST in the following manner : $email = $_POST['email']
I have checked and corrected your code. It was all good, except that you had to declare the POST variables :
<?php
$body = "From: $name
E-Mail: $email
Message:
$message";
$name = $_POST['name'];
$email = $_POST['email'];
$human = $_POST['human'];
$from = 'example@example.com';
if ($_POST['submit']) {
if ($name != '' && $email != '') {
if ($human == 4) {
if (mail ($email, $subject, $body, $from)) {
echo '<p>Your message has been sent!</p>';
} else {
echo '<p>Something went wrong, go back and try again!</p>';
}
} else if ($_POST['submit'] && $human != '4') {
echo '<p>You answered the anti-spam question incorrectly!</p>';
}
} else {
echo '<p>You need to fill in all required fields!</p>';
}
}
?>
You should also check if whether the form fields are submitted or not by using isset() and !empty() functions.