提问者:小点点

使用jQuery,AJAX,PHP的联系表单不在电子邮件中发送字段值


我一直在看所有类似的问题,但还是找不到我的问题。

我从联系人表单收到空白电子邮件。。。

以下是html联系人表单:

<form id="main-contact-form" name="contact-form" method="post" action="sendmail.php">
<div class="form-group">
<input id="name" type="text" name="name" class="form-control" placeholder="Nom" required>
</div>
<div class="form-group">
<input type="email" name="email" class="form-control" placeholder="Email" required>
</div>
<div class="form-group">
<input type="text" name="subject" class="form-control" placeholder="Sujet" required>
</div>
<div class="form-group">
<textarea name="message" class="form-control" rows="8" placeholder="Message" required  </textarea>
</div>
<button type="submit" class="btn btn-primary">Envoyer</button>
</form>

下面是PHP文件:

<?php
$name       = @trim(stripslashes($_POST['name'])); 
$from       = @trim(stripslashes($_POST['email'])); 
$subject    = @trim(stripslashes($_POST['subject'])); 
$message    = @trim(stripslashes($_POST['message'])); 
$to         = 'info@pca.ch';

$headers   = array();
$headers[] = "MIME-Version: 1.0";
$headers[] .= "Content-type: text/plain; charset=utf8";
$headers[] .= "From: {$name} <{$from}>";
$headers[] .= "Reply-To: <{$from}>";
$headers[] .= "Subject: {$subject}";
$headers[] .= "X-Mailer: PHP/".phpversion();

mail($to, $subject, $message, $headers);

?>

下面是jquery代码:

// Contact form
var form = $('#main-contact-form');
form.submit(function(event){
    event.preventDefault();
    var form_status = $('<div class="form_status"></div>');
    $.ajax({
        url: $(this).attr('action'),
        beforeSend: function(){
            form.prepend( form_status.html('<p><i class="fa fa-spinner fa-spin"></i> Envoi...</p>').fadeIn() );
        }
    }).done(function(data){
        form_status.html('<p class="text-success">Votre message a bien été envoyé. Nous vous contacterons dès que possible.</p>').delay(3000).fadeOut();
    });
});

网址为www.pca.ch(法文)

如果我序列化,它还可以,但是我被困在www.pca.ch/sendmail.php中,没有得到确认消息。

非常感谢您的帮助! 奥拉齐奥

我能做到的最好是:

添加serialize:url:$(this).attr('action'),$(this).serialize(),

但是我需要删除php中的$headers,否则会出现错误。

问题是我在行中没有得到确认:})。done(function(data){form_status.html('votre message a bienétéemployeé.Nous vous contacterons dès que opossible。

null

而且大多数情况下,在www.pca.ch/sendmail.php空白页上获得了股票!!


共1个答案

匿名用户

我想你在

$headers   = array();
$headers[] = "MIME-Version: 1.0";
$headers[] .= "Content-type: text/plain; charset=utf8";
$headers[] .= "From: {$name} <{$from}>";
$headers[] .= "Reply-To: <{$from}>";
$headers[] .= "Subject: {$subject}";
$headers[] .= "X-Mailer: PHP/".phpversion();

应该是这样的

$headers = array(
    'From' => 'webmaster@example.com',
    'Reply-To' => 'webmaster@example.com',
    'X-Mailer' => 'PHP/' . phpversion()
);