Forms problems

0

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;
}
    
asked by anonymous 29.10.2017 / 04:31

1 answer

0

I think you should retype your JavaScript code, see this example:

$( "#formulario" ).submit(function( event ) {
  event.preventDefault();
  $.ajax({
    method: "POST",
    url: $(this).attr('action'),
    data: { nome: $("[name='nome']").val(), msg: $("[name='msg']").val() }
  })
  .done(function( data ) {
    console.log(data.form);
  });
  
});
input[type='text'] {
  letter-spacing: 1px;
  padding: 8px 4px;
  width: 250px;
}

input[type='submit'] {
  background: #069;
  border: 0px;
  color: #fff;
  letter-spacing: 1px;
  padding: 8px 24px;
  text-transform: uppercase;
}
<script src="https://code.jquery.com/jquery-2.2.4.js"></script><formid="formulario" action="https://httpbin.org/post">
  <p><input type="text" name="nome" value="Stackoverflow Português :)"></p>
  <p><input type="text" name="msg" value="O script está funcionando :D"></p>
  <p><input type="submit" value="Enviar"></p>
</form>
    
29.10.2017 / 05:06