我无法理解为什么我的PHP联系表单发送空电子邮件。 请帮我弄清问题所在。
HTML表单代码:
<form id="main-contact-form" name="contact-form" method="post" action="sendemail.php">
<div class="form-group">
<input type="text" name="name" class="form-control" required="required" placeholder="Имя">
</div>
<div class="form-group">
<input type="phone" name="phone" class="form-control" required="required" placeholder="Номер телефона">
</div>
<div class="form-group">
<input type="email" name="email" class="form-control" required="required" placeholder="Email">
</div>
<div class="form-group">
<textarea name="message" id="message" required="required" class="form-control" rows="8" placeholder="Ваши пожелания"></textarea>
</div>
<div class="form-group">
<input type="submit" name="submit" class="btn btn-submit" value="Отправить">
</div>
</form>
PHP文件:
<?php
$name = @trim(stripslashes($_POST['name']));
$from = @trim(stripslashes($_POST['email']));
$phone = @trim(stripslashes($_POST['phone']));
$subject = @trim(stripslashes($_POST['subject']));
$message = @trim(stripslashes($_POST['message']));
$to = 'hello@helloschool.ru';
$headers = array();
$headers[] = "MIME-Version: 1.0";
$headers[] .= "Content-type: text/plain; charset=iso-8859-1";
$headers[] .= "From: {$name} <{$from}>";
$headers[] .= "Reply-To: <{$from}>";
$headers[] .= "Subject: {$subject}";
$headers[] .= "X-Mailer: PHP/".phpversion();
mail($to, $subject, $message, $headers);
die;
删除'@'我的错误日志后说:
[Sun Aug 27 21:41:14 2017] [warn] [client 89.169.84.160] mod_fcgid: stderr:PHP注意:未定义的索引:名称在 第2行的/var/www/u0337516/data/www/helloschool.ru/sendemail.php, referer:http://helloschool.ru/
JS代码(main.js):
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> Отправка заявки...</p>').fadeIn() );
}
}).done(function(data){
form_status.html('<p class="text-success">Спасибо, что оставили заявку! Мы свяжемся с вами как можно скорее.</p>').delay(3000).fadeOut();
});
});
提前致谢并抱歉新手问题。
答案 0 :(得分:0)
1)您在php代码中有错误。参数headers
必须是字符串。
$headers = array();
$headers[] = "MIME-Version: 1.0";
$headers[] .= "Content-type: text/plain; charset=iso-8859-1";
$headers[] .= "From: {$name} <{$from}>";
$headers[] .= "Reply-To: <{$from}>";
$headers[] .= "Subject: {$subject}";
$headers[] .= "X-Mailer: PHP/".phpversion();
必须是
$headers = '';
$headers .= "MIME-Version: 1.0"."\r\n";
$headers .= "Content-type: text/plain; charset=iso-8859-1"."\r\n";
$headers .= "From: {$name} <{$from}>"."\r\n";
$headers .= "Reply-To: <{$from}>"."\r\n";
$headers .= "Subject: {$subject}"."\r\n";
$headers .= "X-Mailer: PHP/".phpversion();
2)你的message
是俄语,但代码页iso-8859-1只支持拉丁字母,你需要西里尔语。尝试使用HTML的代码页(我认为它是Windows-1251或UTF-8)。
3)您的javascript
出错了。
$.ajax({
url: form.attr('action')});
必须是
$.ajax({
url: form.attr('action'),
data: form.serialize(),
method: "POST"});