POST method does not take FORM value

Asked

Viewed 1,749 times

9

I’m making a contact form on a website, but the PHP method does not take the value of input in the form, I’ve searched the internet and I can’t find anyone with the same problem.

And e-mail is being sent, the value of the field $subject that I hand come 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 the 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 email message successfully sent, and when I take it off, 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 as it was, but that the data be sent to the 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;
});
  • Of the one print_r($_POST); die('...'); in the first line of sedmail.php and see what appears.

  • Nothing appears, comes empty.

  • Nothing appears or returns an empty array?

  • Nothing appears. I put to print the $_POST in the div where it shows the success message in HTML, and also nothing appears.

  • 1

    How do you know the email is sent? by $status?, check the file name and action of a print_r on $_REQUEST.

  • I get the email. But with the fields empty. I added more information, which seems to be the focus of the problem.

Show 1 more comment

2 answers

5


If sending is done by ajax do not forget to send all the form fields 'manually', setting 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 didn’t understand it very well. I don’t have much knowledge in AJAX. I used this script you sent me, and it didn’t work. Could you give me a hand creating this script that sends the fields manually? And thanks for the help.

2

I discovered the problem from the answer of the rray. The attribute needed to be serialized. 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;
});

Browser other questions tagged

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