0
I have the following script below that does a validation in the contact form and shows the message in div how the message was sent or not, as this below is sent the normal message in my email, but in that same form I have an attachment field and when I try to send an attachment I get nothing in my email.
// Contact form
var form = $('#main-contact-form');
form.submit(function(event){
event.preventDefault();
var form_status = $('<div class="form_status"></div>');
$.ajax({
type: "POST",
url: $(this).attr('action'),
data: $("#main-contact-form").serialize(),
beforeSend: function(){
form.prepend( form_status.html('<p><i class="fa fa-spinner fa-spin"></i> Enviando Mensagem...</p>').fadeIn() );
}
}).done(function(data){
form_status.html('<p class="text-success">Mensagem enviada. O mais breve possível retornaremos o contato.</p>').delay(9000).fadeOut();
});
});
Form
<form enctype="multipart/form-data" name="contact-form" action="envia.php" method="post" id="main-contact-form"> <!-- chama o envia.php -->
<!--<form id="main-contact-form" name="contact-form" method="post" form action="envia.php"> -->
<div class="form-group">
<input type="text" input name="nomeremetente" id="nomeremetente" class="form-control" placeholder="Nome" required>
</div>
<div class="form-group">
<input type="telefone" input name="telefone" class="form-control" placeholder="DDD + Telefone" required>
</div>
<div class="form-group">
<input type="empresa" input name="empresa" class="form-control" placeholder="Empresa" required>
</div>
<div class="form-group">
<input type="cidade" input name="cidade" class="form-control" placeholder="Cidade" required>
</div>
<div class="form-group">
<input type="email" input name="emailremetente" class="form-control" placeholder="E-mail" required>
</div>
<div class="form-group">
<!--<input type="text" input name="assunto" class="form-control" placeholder="Assunto" required>-->
<select id="mySelect" input name="assunto" size="1" class="form-control" required>
<option value="Sem Assunto">Selecione o Assunto</option>
<option value="Comercial">Comercial</option>
<option value="Cotação">Cotação</option>
<option value="Informação">Informação</option>
<option value="Reclamação">Reclamação</option>
<option value="Sugestão">Sugestão</option>
<option value="Trabalhe Conosco">Trabalhe Conosco</option>
<option value="Outros">Outros</option>
</select>
</div>
<div class="form-group" id="inputOculto">
<input id="anexo" name="arquivo" type="file" class="form-control-anexo" input/>
</div>
<div class="form-group">
<textarea input name="mensagem" class="form-control" rows="3" placeholder="Mensagem" required></textarea>
</div>
<button type="submit" class="btn btn-primary">Enviar Mensagem</button>
</form>
Yes, it already has this tag, so much so that if I try to send the attachment without going through the js it send normal.
– William Demski
With the formData worked?
– Felipe
I’m going to search this formData, I have no knowledge in this function, have some example that can help me?
– William Demski
Opa now that I saw that you sent a script, I will try and already return.
– William Demski
It didn’t work out the way you did either.
– William Demski
You used : new Formdata($("#main-contact-form"), ou new Formdata($("#main-contact-form"),? I just forgot a parenthesis there.
– Felipe
I used thus date: new Formdata($("#main-contact-form")) and also data = new Formdata($('#main-contact-form").get(0);.. in both nor came to call the shipment.
– William Demski
Can you see any error message on the console?
– Felipe
Try new Formdata($("#main-contact-form")[0])
– Felipe
I tried this way that Voce passed and also did not give, on the console appears this: Typeerror: 'append' called on an Object that does not implement interface Formdata.
– William Demski
I made a change to the answer by adding this . data: new Formdata(this), processData: false, contenttype: false, .
– Felipe
Bah thank you so much, so now it worked right, sending both with attachment and no attachment, thanks even, I was a week searching in a lot of forum and could not solve. big hug.
– William Demski
If the answer was helpful, mark as answered to help other people
– Felipe
Bah excuse, only detail that the message field is going blank in both cases: Completed Contact Form on the Site Name: William E-mail: [email protected] Telephone: 48996012764 Company: Tsa City: Criciuma SC Subject: Work with us Message: sera is why the field where the attachment goes before the message field?
– William Demski
By placing the attachment field at the end of the form the problem has been solved. Thanks for the help.
– William Demski
Good, have at your disposal
– Felipe
Sorry ignorance but I’m new on the forum, as I mark as answered?
– William Demski
Below where you vote the answer
– Felipe
To mark an answer as accepted, click the check mark on the left side of the answer; its color will change from gray to green.
– Felipe