POST method does not take form value
I am making a contact form on a website, but the PHP method does not take the value of input
in form
, I have already searched the internet and could not find anyone with the same problem.
And email is being sent, the value of the field $subject
that I set in 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>
Follows 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: Looks like I found the focus of the problem, is there a javascript to display the email message sent successfully, and when I shoot it, the email sends normally, but does not appear the message that the email was sent, directs to a page that prints the array. I would like to keep it as it was, but for the data to be sent pro 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;
});
2 answers
If the submission is made by ajax do not forget to send all the 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);
});
I discovered the problem from the rray response. It was missing to serialize the attribute. 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;
});