我需要帮助从html页面上的表单发送邮件的价值, 有3个文件:test.html with form group复选框,contact.php和contact.js
这是contact.js:
$(function () {
$('#contact-form').validator();
$('#contact-form').on('submit', function (e) {
if (!e.isDefaultPrevented()) {
var url = "contact.php";
$.ajax({
type: "POST",
url: url,
data: $(this).serialize(),
success: function (data)
{
var messageAlert = 'alert-' + data.type;
var messageText = data.message;
var alertBox = '<div class="alert ' + messageAlert + ' alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>' + messageText + '</div>';
if (messageAlert && messageText) {
$('#contact-form').find('.messages').html(alertBox);
$('#contact-form')[0].reset();
}
}
});
return false;
}
})
});
这是用于处理表单到邮件的contact.php
$fields = array('name' => 'Name', 'surname' => 'Surname', 'phone' => 'Phone', 'email' => 'Email', 'message' => 'Message' 'checkboxes' => 'Colors');
表单组是:
<div class="form-group">
<div class="col-md-12">
<label>Colors:</label>
</div>
<div class="col-md-12">
<label class="checkbox-inline" for="form_checkboxes1">
<input type="checkbox" name="checkboxes[]" id="form_checkboxes1" value="Blue"> + Blue
</label>
<label class="checkbox-inline" for="form_checkboxes2">
<input type="checkbox" name="checkboxes[]" id="form_checkboxes2" value="Green"> + Green
</label>
<label class="checkbox-inline" for="form_checkboxes3">
<input type="checkbox" name="checkboxes[]" id="form_checkboxes3" value="Orange"> + Orange
</label>
</div>
问题是当选择颜色选项或在邮件发送后选择每三种颜色结果为Colors:array
现在,我试图在contact.php添加
中更改行$checkboxes = implode(',',$_POST['checkboxes']);
但没有succsess
contact.php中的其余部分是:
try{
$emailText = "text";
foreach ($_POST as $key => $value) {
if (isset($fields[$key])) {
$emailText .= "$fields[$key]: $value\n";}
}
mail($sendTo, $subject, $emailText, "From: " . $from);
$responseArray = array('type' => 'success', 'message' => $okMessage);
}
答案 0 :(得分:0)
试试这段代码,它适合您。 你的代码没有工作,因为你有颜色传递数组,所以你需要内爆你的数组,然后paas到你的邮件。 我试过这个,它的工作正常。
try{
$emailText = "text";
foreach ($_POST as $key => $value) {
if (is_array($value)) {
$emailText .= "$fields[$key]: " . implode(',', $value) . "\n";
continue;
}
if (isset($fields[$key])) {
$emailText .= "$fields[$key]: $value\n";}
}
mail($sendTo, $subject, $emailText, "From: " . $from);
$responseArray = array('type' => 'success', 'message' => $okMessage);
}