POST method does not take FORM value

8

I'm making a contact form on a website, but the PHP method does not get the value of input in form , I already searched the internet and could not find anyone with the same problem.

And email is being sent, the value of the $subject field that I store in my hand comes in the email, the rest is not filled.

Follow my form:

<form id="main-contact-form" class="contact-form" method="post" action="sendemail.php" role="form">
    <div class="row">
        <div class="col-sm-6">
            <div class="form-group">
                <input name="contato-nome" id="contato-nome" type="text" class="form-control" required placeholder="Nome">
            </div>
        </div>
        <div class="col-sm-6">
            <div class="form-group">
                <input name="contato-email" id="contato-email" type="email" class="form-control" required placeholder="E-mail">
            </div>
        </div>
    </div>
    <div class="row">
        <div class="col-sm-12">
            <div class="form-group">
                <textarea name="contato-mensagem" id="contato-mensagem" required class="form-control" rows="8" placeholder="Mensagem" ></textarea>
            </div>
            <div class="form-group">
                <button type="submit" class="btn btn-primary btn-lg">Enviar Mensagem</button>
            </div>
        </div>
    </div>
</form>

Follow sendmail.php

<?php
header('Content-type: application/json');
$status = array(
    'type'=>'success',
    'message'=>'Email enviado!'
);

$name = @trim(stripslashes($_POST["contato-nome"]));
$email = @trim(stripslashes($_POST["contato-email"])); 
$subject = "E-Mail enviado através do site samamba.com.br"; 
$message = @trim(stripslashes($_POST["contato-mensagem"])); 

$email_from = $email;
$email_to = '[email protected]';

$body = 'Nome: ' . $name . "\n\n" . 'Email: ' . $email . "\n\n" . 'Assunto: ' . $subject . "\n\n" . 'Menssagem: ' . $message;

$success = @mail($email_to, $subject, $body, 'From: <'.$email_from.'>');

echo json_encode($status);
die; 

Added: It seems that I found the focus of the problem, there is a javascript to display the successfully sent email message, and when I shot it, the email normally sends but the message does not appear that the email was sent, directs to a page that prints the array. I would like to keep it as it was, but the data would be sent to POST

//Ajax contact
var form = $('.contact-form');
form.submit(function () {
    $this = $(this);
    $.post($(this).attr('action'), function(data) {
        $this.prev().text(data.message).fadeIn().delay(3000).fadeOut();
    },'json');
    return false;
});
    
asked by anonymous 31.07.2015 / 13:32

2 answers

4

If ajax is sent do not forget to send all fields of the form 'manually', defining an attribute and serializing the form by id

 $.post($("#main-contact-form").attr('action'), $("#main-contact-form").serialize)
        .done(function(msg) {
            console.log(msg);
         });
    
31.07.2015 / 14:31
2

I discovered the problem from the rray response. Serializing the attribute was missing. I used the following ajax and it worked correctly!

var form = $('.contact-form');
form.submit(function () {'use strict',
    $this = $(this);    
    $.post($(this).attr('action'), $this.serialize(), function(data) {
        $this.prev().text(data.message).fadeIn().delay(3000).fadeOut();
    },'json');
    return false;
});
    
31.07.2015 / 18:53