I'm trying to send email from my page, but it does not return any message if it succeeded or not, I'm using php along with bootstrapvalidation and ajax to forward the information to my php page.
JS
// Contact Form Scripts
$(function() {
$("#contactForm input,#contactForm textarea").jqBootstrapValidation({
preventSubmit: true,
submitError: function($form, event, errors) {
// additional error messages or events
},
submitSuccess: function($form, event) {
event.preventDefault(); // prevent default submit behaviour
// get values from FORM
var name = $("input#name").val();
var email = $("input#email").val();
var assunto = $("input#assunto").val();
var message = $("textarea#message").val();
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({
url: "././mail/contact_me.php",
type: "POST",
data: {
name: name,
assunto:assunto,
email: email,
message: message
},
cache: false,
success: function() {
// Success message
$('#success').html("<div class='alert alert-success'>");
$('#success > .alert-success').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>×")
.append("</button>");
$('#success > .alert-success')
.append("<strong>Sua mensagem foi enviada, em breve entraremos em contato.. </strong>");
$('#success > .alert-success')
.append('</div>');
//clear all fields
$('#contactForm').trigger("reset");
},
error: function() {
// Fail message
$('#success').html("<div class='alert alert-danger'>");
$('#success > .alert-danger').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>×")
.append("</button>");
$('#success > .alert-danger').append("<strong>Sorry " + firstName + ", it seems that my mail server is not responding. Please try again later!");
$('#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('');
});
HTML
<form action="mail/contact_me.php" method="post" name="sentMessage" id="contactForm" novalidate>
<div class="row">
<div class="form-group col-md-6">
<input type="text" class="form-control" placeholder="Nome *" id="name" required data-validation-required-message="Por favor, informe seu nome.">
<p class="help-block text-danger"></p>
</div>
<div class="form-group col-md-6">
<input type="email" class="form-control" placeholder="E-mail *" id="email" required data-validation-required-message="Por favor, informe seu e-mail.">
<p class="help-block text-danger"></p>
</div>
<div class="form-group col-md-12">
<input type="text" name="assunto" id="assunto" class="form-control" placeholder="ASSUNTO*" required data-validation-required-message="Por favor, informe um assunto.">
<p class="help-block text-danger"></p>
</div>
<div class="col-md-12">
<div class="form-group">
<textarea class="form-control" rows="5" placeholder="Mensagem *" id="message" required data-validation-required-message="Por favor, insira uma mensagem."></textarea>
<p class="help-block text-danger"></p>
</div>
</div>
<div class="clearfix"></div>
<div class="form-group">
<div id="success">teste</div>
<button type="submit" class="btn btn-primary pull-right">ENVIAR MENSAGEM</button>
</div>
</div>
</form>
PHP
<?php
/*Checa se os campos estao vazios*/
if (empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['assunto']) ||
empty($_POST['message']) ||
!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
echo "No arguments Provided!";
return false;
}
$name = strip_tags(htmlspecialchars($_POST['name']));
$email_address = strip_tags(htmlspecialchars($_POST['email']));
$assunto = strip_tags(htmlspecialchars($_POST['assunto']));
$message = strip_tags(htmlspecialchars($_POST['message']));
/*Configurações de envio de Email*/
$to = '[email protected]'; /*Email de Recebimento*/
$email_subject = "Formulario de Contato"; /*Assunto do Email*/
$email_body = "Nome: $name\n"
. "Email: $email_address\n"
. "Assunto: $assunto\n\n"
. "Mensagem:\n$message";
$headers = "From: " . $email_address . "\n"; /*Email de quem enviou*/
$headers .= "Reply-To: $email_address";
mail($to, $email_subject, $email_body, $headers);
return true;