我希望名称字段的值显示在我网站用户的邮件主题中。我正在使用phpmailer插件。 目前,显示“来自用户的消息”。
handler.php:
$pp = new FormHandler();
$validator = $pp->getValidator();
$validator->fields(['name','email'])->areRequired()->maxLength(50);
$validator->field('email')->isEmail();
$validator->field('message')->maxLength(6000);
$validator->CharSet = 'UTF-8';
FormHandler.php:
public function __construct()
{
$this->emails = array();
$this->validator = FormValidator::create();
$this->mailer = new PHPMailer;
$this->mail_template='';
$this->mailer->Subject = "Message from the user";
$host = isset($_SERVER['SERVER_NAME'])?$_SERVER['SERVER_NAME']:'localhost';
$from_email ='forms@'.$host;
$this->mailer->setFrom($from_email,'BanerBunny.pl',false);
$this->captcha = false;
$this->attachments = [];
$this->recaptcha =null;
}
和html:
<div id="form-main">
<div id="form-div">
<form class="montform" id="reused_form" enctype="multipart/form-data">
<p class="name">
<input name="name" type="text" class="feedback-input" placeholder="Imię i Nazwisko"
id="name" required/>
</p>
</form>
</div>
</div>
答案 0 :(得分:0)
相反:
$this->mailer->Subject = "Message from the user";
尝试使用:
$this->mailer->Subject = ($_POST['name']);
它应该可以正常工作。
答案 1 :(得分:-1)
表单通过JQuery / AJAX提交代码 是的,我尝试使用这个变量:$ this-&gt; mailer-&GT; Subject = $ validator-&gt;字段(&#39; name&#39;)。不幸的是,表格根本没有发出。
form.js
$(function () {
function after_form_submitted(data) {
if (data.result == 'success') {
$('form#reused_form').hide();
$('#success_message').css('display', 'flex');
$('#error_message').hide();
}
else {
$('#error_message').append('<ul></ul>');
jQuery.each(data.errors, function (key, val) {
$('#error_message ul').append('<li>' + key + ':' + val + '</li>');
});
$('#success_message').hide();
$('#error_message').show();
//reverse the response on the button
$('button[type="button"]', $form).each(function () {
$btn = $(this);
label = $btn.prop('orig_label');
if (label) {
$btn.prop('type', 'submit');
$btn.text(label);
$btn.prop('orig_label', '');
}
});
}//else
}
$('#reused_form').submit(function (e) {
e.preventDefault();
$form = $(this);
//show some response on the button
$('button[type="submit"]', $form).each(function () {
$btn = $(this);
$btn.prop('type', 'button');
$btn.prop('orig_label', $btn.text());
$btn.text('Wysyłanie ...');
});
var formdata = new FormData(this);
$.ajax({
type: "POST",
url: 'handler.php',
data: formdata,
success: after_form_submitted,
dataType: 'json',
processData: false,
contentType: false,
cache: false
});
});