i wrote code for contact form and i dont know why it doesnt work. Everything is set up with form but i didnt receive email. I didnt get any error or something. This is the html code :
<!-- Form -->
<form method="post" action="contact_form.php" name="contactform" id="contactform" class="row">
<fieldset>
<div class="form-field col-sm-6">
<label for="name">Name</label>
<span><input type="text" name="name" id="name" /></span>
</div>
<div class="form-field col-sm-6">
<label for="email">Email</label>
<span><input type="email" name="email" id="email" /></span>
</div>
<div class="form-field col-sm-6">
<label>*What is 2+2? (Anti-spam)</label>
<span><input name="human" placeholder="Type Here"></span>
</div>
<div class="form-field col-sm-12">
<label for="message">Message</label>
<span><textarea name="message" id="message"></textarea></span>
</div>
</fieldset>
<div class="form-click center col-sm-12">
<span><input type="submit" name="submit" value="Send it" id="submit" /></span>
</div>
<div id="alert" class="col-sm-12"></div>
</form>
This is the php code :
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: TangledDemo';
$to = 'kreso.galic8@gmail.com';
$subject = 'Hello';
$human = $_POST['human'];
$body = "From: $name\n E-Mail: $email\n Message:\n $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>';
}
}
?>
It seems that if ($_POST['submit']) {
is not working.
You should use empty
or isset
:
if (!empty($_POST['submit'])) {
or
if (isset($_POST['submit'])) {
Be careful, if the user press enter in an input field instead of clicking on the submit button, the submit value won't be send.
You could use a hidden field instead:
<input type="hidden" name="ok" value="1" />
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.