Event.preventDefault() for attachment with php and js

Asked

Viewed 227 times

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> 

1 answer

1


First check in the form statement if you have the tag enctype="multipart/form-data" then try Using the class Formdata

Ex

 $.ajax({
        type: "POST",
        url: $(this).attr('action'),
        data: new FormData(this),
        processData: false,
        contentType: false,
        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();
    });
  • 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.

  • With the formData worked?

  • I’m going to search this formData, I have no knowledge in this function, have some example that can help me?

  • Opa now that I saw that you sent a script, I will try and already return.

  • It didn’t work out the way you did either.

  • You used : new Formdata($("#main-contact-form"), ou new Formdata($("#main-contact-form"),? I just forgot a parenthesis there.

  • 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.

  • Can you see any error message on the console?

  • Try new Formdata($("#main-contact-form")[0])

  • 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.

  • I made a change to the answer by adding this . data: new Formdata(this), processData: false, contenttype: false, .

  • 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.

  • If the answer was helpful, mark as answered to help other people

  • 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?

  • By placing the attachment field at the end of the form the problem has been solved. Thanks for the help.

  • Good, have at your disposal

  • Sorry ignorance but I’m new on the forum, as I mark as answered?

  • Below where you vote the answer

  • 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.

Show 14 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.