我正在使用PHPMailer脚本从用户填写的表单中发送电子邮件,然后用户从下拉列表中选择一个图像,然后该表单根据用户选择将相应的图像与电子邮件一起发送。所以它非常简单,我让它工作了一段时间,然后突然停止工作了,我什么也记不清了。我也在使用bootstrap和DDSlick(用于下拉列表)。
映像是错误日志中未定义的内容,所有其他信息都起作用!
这是错误:
Undefined index: image in /home/name/website/assets/php/store-email.php on line 8
HTML:
<form id="contact-form" method="post" role="form">
<div class="messages"></div>
<div class="controls">
<div class="row">
<div class="col-md-12">
<div class="form-group">
<input id="form_name" type="text" name="name" class="form-control popup_custom-fields" placeholder="Please enter your Name *" required="required" data-error="Name is required.">
<div class="help-block with-errors"></div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="form-group popup_custom-fields" id="demo-htmlselect">
<select type="text" name="image" id="image" class="form-control " required="required">
<option value="select">--Select-- *</option>
<option value="image1" data-imagesrc="assets/images/gallery/gallery-1.jpg">Image 1</option>
<option value="image2" data-imagesrc="assets/images/gallery/gallery-2.jpg">image 2</option>
<option value="image3" data-imagesrc="assets/images/gallery/gallery-3.jpg">image 3</option>
<option value="image4" data-imagesrc="assets/images/gallery/gallery-4.jpg">image 4</option>
<option value="image5" data-imagesrc="assets/images/gallery/gallery-5.jpg">image 5</option>
<option value="image6" data-imagesrc="assets/images/gallery/gallery-6.jpg">image 6</option>
<option value="image7" data-imagesrc="assets/images/gallery/gallery-7.jpg">image 7</option>
<option value="image8" data-imagesrc="assets/images/gallery/gallery-8.jpg">image 8</option>
</select>
<div class="help-block with-errors"></div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="form-group">
<input id="form_email" type="email" name="email" class="form-control popup_custom-fields" placeholder="Please enter your email *" required="required" data-error="Valid email is required.">
<div class="help-block with-errors"></div>
</div>
</div>
<div class="col-md-12">
<div class="form-group">
<input id="form_phone" type="tel" name="tel" class="form-control popup_custom-fields" placeholder="Please enter your phone">
<div class="help-block with-errors"></div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="form-group">
<textarea id="form_message" name="mssg" class="form-control popup_custom-fields" placeholder="Message for me *" rows="4" required="required" data-error="Please,leave us a message."></textarea>
<div class="help-block with-errors"></div>
</div>
</div>
<div class="col-md-12">
<input type="submit" class="btn btn-success btn-send popup_custom-button" value="Send message">
</div>
</div>
</div>
</form>
AJAX:
$(function () {
$('#contact-form').validator();
$('#contact-form').on('submit', function (e) {
if (!e.isDefaultPrevented()) {
var url = "../assets/php/store-email.php";
$.ajax({
type: "POST",
url: url,
dataType:'json',
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;
}
})
});
PHP:
<?php
require 'PHPMailerAutoload.php';
header('Content-Type: application/json');
$fname = $_POST['name'];
$mail = $_POST['email'];
$message = $_POST['mssg'];
$tel = $_POST['tel'];
$image = $_POST['image']; // LINE 8
$mail = new PHPMailer;
$mail->isSMTP(); // Set mailer
to use SMTP
$mail->Host = 'localhost'; // Specify main and backup SMTP servers
$mail->Port = 465; // TCP port to
connect to
$mail->SMTPSecure = 'ssl';
$mail->setFrom("[email protected]");
// Add a recipient
$mail->addAddress('[email protected]');
// Name is optional
$mail->isHTML(true); // Set email
format to HTML
// Add attachments
if ($image == 'image1') {
$mail->AddAttachment($_SERVER['DOCUMENT_ROOT']."assets/images/gallery/gallery-1.jpg");
}
else if ($image == 'image2') {
$mail->addAttachment('../images/gallery/gallery-2.jpg');
}
else if ($image == 'image3') {
$mail->addAttachment('../images/gallery/gallery-3.jpg');
}
else if ($image == 'image4') {
$mail->addAttachment('../images/gallery/gallery-4.jpg');
}
else if ($image == 'image5') {
$mail->addAttachment('../images/gallery/gallery-5.jpg');
}
else if ($image == 'image6') {
$mail->addAttachment('../images/gallery/gallery-6.jpg');
}
else if ($image == 'image7') {
$mail->addAttachment('../images/gallery/gallery-7.jpg');
}
else if ($image == 'image8') {
$mail->addAttachment('../images/gallery/gallery-8.jpg');
}
else {
$mail->addAttachment('../images/gallery/gallery-none.jpg');
}
$mail->Subject = 'New Message From Website!';
$mail->Body= "$message Phone: $tel";
$mail->AltBody = $message;
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
?>
最佳答案
事实证明DDSlick是导致无法发送图像的原因。
尝试在您的Ajax请求之前添加以下内容:
$(document).find(".dd-selected-value").attr("name","image");
这会将我们正在寻找的缺少名称属性添加到DDSlick用于所选“选项”的隐藏输入中。
并且图像信息将与其他信息一起发送。