0
I’m trying to make a form using js and php, only when I click the send button, it always stays with "Sending email..." and never sends, I checked console and says that the error is in email.php (404 error not found)... If anyone can help me, follow below 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;
}
The archive
email.php
is there ? It is in the same directory as this file HTML ?– NoobSaibot
it gets, this php I put there is email.php
– darknone
File name is correct ? Because error
404
want that does not exist or not found, practically the same thing. Is using.htaccess
? If yes, ask the question.– NoobSaibot
Yes, I have checked several times and even renamed, but it is correct yes :c, I am not using htacess
– darknone
What is the complete path that appears in
Console
on the flap Network ?– NoobSaibot
Within your
ajax
has this codeurl: $(this).attr('action'),
prints it on the console and see if it is loading the correct link fromemail.php
,console.log($(this).attr('action'));
– WMomesso
only gave Undefined
– darknone
Your error is not in Ajax or anything, it is exclusively in PHP. Your email sending code does not work, it is returning error, so your form does not advance. Review this sending code because it is wrong.
– Sam