danog.github.io/js/contact_me.js

87 lines
3.8 KiB
JavaScript
Raw Normal View History

2015-08-27 23:06:33 +02:00
$(function() {
2016-02-17 14:05:07 +01:00
$("#contactForm input,#contactForm textarea").jqBootstrapValidation({
2015-08-27 23:06:33 +02:00
preventSubmit: true,
submitError: function($form, event, errors) {
// additional error messages or events
},
submitSuccess: function($form, event) {
// Prevent spam click and default submit behaviour
$("#btnSubmit").attr("disabled", true);
event.preventDefault();
2016-02-17 14:05:07 +01:00
2015-08-27 23:06:33 +02:00
// get values from FORM
var name = $("input#name").val();
var email = $("input#email").val();
var phone = $("input#phone").val();
var message = $("textarea#message").val();
2015-08-29 21:34:50 +02:00
var domain = "1";
2015-08-27 23:06:33 +02:00
var firstName = name; // For Success/Failure Message
// Check for white space in name for Success/Fail message
if (firstName.indexOf(' ') >= 0) {
firstName = name.split(' ').slice(0, -1).join(' ');
}
$.ajax({
2015-08-29 21:34:50 +02:00
url: "https://mail.daniil.it/",
2015-08-27 23:06:33 +02:00
type: "POST",
data: {
name: name,
phone: phone,
email: email,
2015-08-29 22:09:40 +02:00
message: message,
2015-08-29 21:34:50 +02:00
domain: domain
2015-08-27 23:06:33 +02:00
},
cache: false,
2016-02-17 14:05:07 +01:00
success: function(data) {
$("#btnSubmit").attr("disabled", false);
2016-02-17 12:45:54 +01:00
if(data == "ok") {
// Success message
$('#success').html("<div class='alert alert-success'>");
$('#success > .alert-success').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;")
.append("</button>");
$('#success > .alert-success')
2016-02-17 14:10:38 +01:00
.append("<strong>The message has been sent. </strong>");
2016-02-17 12:45:54 +01:00
$('#success > .alert-success')
.append('</div>');
//clear all fields
$('#contactForm').trigger("reset");
} else {
// Fail message
$('#success').html("<div class='alert alert-danger'>");
$('#success > .alert-danger').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;")
.append("</button>");
2016-02-17 14:10:38 +01:00
$('#success > .alert-danger').append("<strong>Sorry " + firstName + ", it looks like an error occurred. Please try again later!</strong>");
2016-02-17 12:45:54 +01:00
$('#success > .alert-danger').append('</div>');
//clear all fields
$('#contactForm').trigger("reset");
}
2015-08-27 23:06:33 +02:00
},
error: function() {
2016-02-17 14:05:07 +01:00
$("#btnSubmit").attr("disabled", false);
2015-08-27 23:06:33 +02:00
// Fail message
$('#success').html("<div class='alert alert-danger'>");
$('#success > .alert-danger').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;")
.append("</button>");
2016-02-17 14:10:38 +01:00
$('#success > .alert-danger').append("<strong>Sorry " + firstName + ", it looks like an error occurred. Please try again later!</strong>");
2015-08-27 23:06:33 +02:00
$('#success > .alert-danger').append('</div>');
//clear all fields
$('#contactForm').trigger("reset");
},
})
},
filter: function() {
return $(this).is(":visible");
},
});
$("a[data-toggle=\"tab\"]").click(function(e) {
e.preventDefault();
$(this).tab("show");
});
});
// When clicking on Full hide fail/success boxes
$('#name').focus(function() {
$('#success').html('');
});