I'm trying to make a form using js and php, but when I click the submit button, it always stays with "Sending Email ..." and never sends, I checked the console and says the error is in the email.php (404 error not found) ... If someone can help me, here are the codes I used:
HTML:
<div class="contact-form">
<h3>Contate-nos</h3>
<form id="main-contact-form" name="contact-form" method="post" action="email.php">
<div class="form-group">
<input type="text" name="name" class="form-control" id="name" placeholder="Nome">
</div>
<div class="form-group">
<input type="email" name="email" class="form-control" id="email" placeholder="Email">
</div>
<div class="form-group">
<input type="text" name="subject" class="form-control" id="subject" placeholder="Assunto">
</div>
<div class="form-group">
<textarea name="message" class="form-control" rows="8" id="message" placeholder="Message"></textarea>
</div>
<button type="submit" name="submit" class="btn btn-primary">Enviar Mensagem</button>
</form>
</div>
JS:
<script>
var form = $('#main-contact-form');
form.submit(function(event){
event.preventDefault();
var form_status = $('<div class="form_status"></div>');
var data = {
name: $('#name').val(),
email: $('#email').val(),
subject: $('#subject').val(),
message: $('#message').val()
};
$.ajax({
url: $(this).attr('action'),
type: "POST",
dataType: "json",
data: {'data': data},
beforeSend: function(){
form.prepend( form_status.html('<p><i class="fa fa-spinner fa-spin"></i> Enviando email...</p>').fadeIn() );
},
success: function (data) {
if(data == 1){
form_status.html('<p class="text-success">Sucesso!</p>').delay(3000).fadeOut();
}
else{
form_status.html('<p class="text-error">Desculpe,
tente novamente!</p>').delay(3000).fadeOut();
}
},
error: function (xhr, status, error) {
console.log(error);
}
});
});
</script>
PHP:
<?php
$data = $_POST['data'];
$name = $data['name'];
$from = $data['email'];
$subject = $data['subject'];
$message = $data['message'];
$to = '[email protected]';
$headers = "MIME-Version: 1.0";
$headers .= "Content-type: text/plain; charset=iso-8859-1";
$headers .= "From: {$name} <{$from}>";
$headers .= "Reply-To: <{$from}>";
$headers .= "Subject: {$subject}";
$headers .= "X-Mailer: PHP/".phpversion();
if(mail($to, $subject, $message, $headers)){
echo 1;
}
else{
echo 0;
}