Why does the php mail () function successfully send mail, but with empty fields?

Email arrives at the destination address, but with blank fields. What is the reason?

My use is mail()as follows:

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

$name = @trim(stripslashes($_POST['name'])); 
$email = @trim(stripslashes($_POST['email'])); 
$subject = @trim(stripslashes($_POST['subject'])); 
$message = @trim(stripslashes($_POST['message'])); 

$email_from = $email;
$email_to = 'info@siteaddress.com';

$body = 'Name: ' . $name . "\n\n" . 'Email: ' . $email . "\n\n" . 'Subject: ' . $subject . "\n\n" . 'Message: ' . $message;

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

echo json_encode($status);
die; 
?>

And the HTML form:

<form id="main-contact-form" class="contact-form" name="contact-form" method="post" action="sendemail.php" role="form">
<div class="row">
<div class="col-sm-6">
<div class="form-group">
<input type="text" class="form-control" required placeholder="Name" name="name" id="name">
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<input type="text" class="form-control" required placeholder="Email address" name="email" id="email">
</div>
</div>
</div>
<div class="row">
<div class="col-sm-12">
<div class="form-group">
<textarea name="message" id="message" required class="form-control" rows="8" placeholder="Message" name="message" id="message"></textarea>
</div>
<div class="form-group">
<button type="submit" class="btn btn-danger btn-lg">Send Message</button>
</div>
</div>
</div>
</form>
+4
source share
4 answers

$subject = @trim(stripslashes($_POST['subject']));, but your form has no theme, you must add it.
Do not suppress @ errors because you will never know what exactly is happening with your code.

+3
source

Finally got the answer .... The problem is that my form did not post anything because the following script was missing

<script>
var form = $('.contact-form');
form.submit(function () {
    $this = $(this);
    $.post($(this).attr('action'),$(this).serialize(), function(data) {
        $this.prev().text(data.message).fadeIn().delay(3000).fadeOut();
    },'json');
    return false;
});
</script>

$(this).serialize() script, ...

....

0

Hi I have the same problem in the 000webhost application. I solve this with two settings of the 1st: Add var name = $ ("input # name"). Val (); for form values ​​and for ajax name: name function, for form values

var form = $('#main-contact-form');
form.submit(function(event){
    event.preventDefault();     
        var name = $("input#name").val();
        var email = $("input#email").val();
        var subject = $("input#subject").val();
        var message = $("textarea#message").val();

    var form_status = $('<div class="form_status"></div>');
    $.ajax({
         url: $(this).attr('action'),
            type: "POST",
            data: {
                name: name,
                email: email,
                subject: subject,
                message: message,
            },
            cache: false,

        beforeSend: function(){
            form.prepend( form_status.html('<p><i class="fa fa-spinner fa-spin"></i> Email enviandose</p>').fadeIn() );

        }
    }).done(function(data){
        form_status.html('<p class="text-success"> Gracias por la consulta, a la brevedad estaremos respondiendo</p>').delay(3000).fadeOut();
             //clear all fields
            $('#main-contact-form').trigger("reset");
    });
});  

2nd: Tag identifier "name" id "email" id "XX" in mi Form in my case

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

     //to Replace This

    $header  .= 'MIME-Version: 1.0' . "\r\n";
    $header .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

    $success = @mail($email_to, $subject, $body, 'From: <'.$email_from.'>',,$header);
 if( $success == true )  
               { 
//success Message
}
else{
//Error Message
}
-1
source

Source: https://habr.com/ru/post/1584174/


All Articles