$('#contact-submit').click(function (e) {
e.preventDefault();
var error = false;
var name = $('#name').val();
var email = $('#email').val();
var subject = $('#subject').val();
var message = $('#message').val();
if (name.length == 0) {
var error = true;
$('#name').css("border-color", "#D8000C");
} else {
$('#name').css("border-color", "#666");
}
if (email.length == 0 || email.indexOf('@') == '-1') {
var error = true;
$('#email').css("border-color", "#D8000C");
} else {
$('#email').css("border-color", "#666");
}
if (subject.length == 0) {
var error = true;
$('#subject').css("border-color", "#D8000C");
} else {
$('#subject').css("border-color", "#666");
}
if (message.length == 0) {
var error = true;
$('#message').css("border-color", "#D8000C");
} else {
$('#message').css("border-color", "#666");
}
if (error == false) {
$('#contact-submit').attr({
'disabled': 'false',
'value': 'Sending...'
});
$.post("sendmail.php", $("#contact-form").serialize(), function (result) {
if (result == 'sent') {
$('#cf-submit').remove();
$('#mail-success').fadeIn(500);
} else {
$('#mail-fail').fadeIn(500);
$('#contact-submit').removeAttr('disabled').attr('value', 'Send The Message');
}
});
}
});
<div class="contact-form col-md-6 wow fadeInUp" data-wow-duration="500ms" data-wow-delay="300ms">
<form id="contact-form" method="post" action="sendmail.php" role="form">
<div class="form-group">
<input type="text" placeholder="Ваше имя" class="form-control" name="name" id="name">
</div>
<div class="form-group">
<input type="email" placeholder="Ваша почта" class="form-control" name="email" id="email">
</div>
<div class="form-group">
<input type="text" placeholder="Номер телефона" class="form-control" name="subject" id="subject">
</div>
<div class="form-group">
<textarea rows="6" placeholder="Сообщение" class="form-control" name="message" id="message"></textarea>
</div>
<div id="mail-success" class="success">
Отправлено, мы свяжемся с вами в ближайшее время
</div>
<div id="mail-fail" class="error">
Извините, но вы допустили где-то ошибку
</div>
<div id="cf-submit">
<input type="submit" id="contact-submit" class="btn btn-transparent" value="Отправить">
</div>
</form>
<?php
$errorMSG = "";
// NAME
if (empty($_POST["name"])) {
$errorMSG = "Name is required ";
} else {
$name = $_POST["name"];
}
// EMAIL
if (empty($_POST["email"])) {
$errorMSG .= "Email is required ";
} else {
$email = $_POST["email"];
}
// MSG SUBJECT
if (empty($_POST["subject"])) {
$errorMSG .= "Subject is required ";
} else {
$msg_subject = $_POST["subject"];
}
// MESSAGE
if (empty($_POST["message"])) {
$errorMSG .= "Message is required ";
} else {
$message = $_POST["message"];
}
$EmailTo = "andrey@r-element.by";
$Subject = "New Message Received";
// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $name;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $email;
$Body .= "\n";
$Body .= "Subject: ";
$Body .= $msg_subject;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $message;
$Body .= "\n";
// send email
$success = mail($EmailTo, $Subject, $Body, "From:".$email);
// redirect to success page
if ($success && $errorMSG == ""){
echo "success";
}else{
if($errorMSG == ""){
echo "Something went wrong :(";
} else {
echo $errorMSG;
}
}
?>