I have problem with sending email from html form. Somehow when i click submit nothing happens. The validation of fields works nevertheless as mentioned before nothing happens when doing submit button. What could be wrong here?
html part:
<section id="contact">
<div class="container">
<h3>Formularz kontaktowy</h3>
<div class="col-md-6 col-md-offset-3 wow fadeInUp" data-wow-delay=".3s">
<form role="form" id="contactForm">
<div class="row">
<div class="form-group">
<input type="text" class="form-control" name="name" id="name"
placeholder="Wpisz swoje imię, nazwisko" required="required">
</div>
<div class="form-group">
<input type="email" class="form-control" name="email" id="email" placeholder="Enter email"
required>
</div>
<div class="form-group">
<textarea id="message" name="message" class="form-control" rows="5"
placeholder="Enter your message" required></textarea>
</div>
<button type="submit" id="form-submit" class="btn-block">Wyślij wiadomość</button>
<div class="alert alert-success h4 hidden" id="msgSubmit">
<strong>Udało się!</strong> Twoja wiadomość została wysłana poprawnie.
</div>
<div class="alert alert-danger h4 hidden" id="msgError">
<strong>Błąd</strong> Prosimy odświeżyć stronę i spróbować ponownie.
</div>
</div>
</form>
</div>
</div>
</section>
Scripts:
<script>
$(document).ready(function () {
$("#contactForm").on("submit", function (event) {
if (event.isDefaultPrevented()) {
} else {
event.preventDefault();
submitForm();
}
});
});
function submitForm() {
$.ajax({
type: "POST",
url: "mail.php",
data: $("#contactForm").serialize(),
success: function (text) {
if (text == "success") {
$("#msgSubmit").removeClass("hidden");
setTimeout(function () {
$("#msgSubmit").addClass("hidden");
}, 6000);
}
},
error: function () {
/* You probably want to add an error message as well */
$("#msgError").removeClass("hidden");
}
});
};
</script>
mail.php file:
<?php
//Required field names array
$required = array('name', 'email', 'message');
//Loop over field names, make sure each one exists and is not empty
$error = false;
foreach($required as $field) {
if (empty($_POST[$field])) {
$error = true;
}
}
if ($error)
{
echo "invalid"; //Was empty values should not be reached as html form inspecting it as well
}
else
{
$name = $_POST["name"];
$email = $_POST["email"];
$message = $_POST["message"];
$EmailTo = "[email protected]";
$Subject = $name;
$message .= "nn" . 'From: '. $email;
$success = mail($EmailTo, $Subject, $message);
//Results catched by AJAX
if ($success){
echo "success";
}else
{
echo "invalid";
}
}
?>