【问题标题】:Undefined Index PHPMailer Form未定义索引 PHPMailer 表单
【发布时间】:2017-06-29 01:52:19
【问题描述】:

我正在使用 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">&times;</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@email.com");
 // Add a recipient
 $mail->addAddress('email@email.com');               
 // 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';
 }
 ?>

【问题讨论】:

  • 发布您遇到的确切错误...在您发布的代码中email@email.com 之后缺少双引号"...可能只是您的问题中的一个错字...
  • @LouysPatriceBessette 添加
  • 该错误表明$_POSTimage 索引未定义。我唯一能看到的是你在&lt;select&gt; 元素上的type="text"...它肯定没有用,但我不知道这是否是原因...
  • @LouysPatriceBessette 我想通了,是插件 DDslick 删除了我的 name 属性。我得再去看看,直到你问我才想到!谢谢你的帮助!
  • 哈哈...这是一个问题。好的。 ;)

标签: javascript php jquery ajax phpmailer


【解决方案1】:

原来DDSlick是导致图片无法发送的原因。

尝试在您的 Ajax 请求之前添加:

$(document).find(".dd-selected-value").attr("name","image");

这会将我们正在寻找的缺失名称属性添加到 DDSlick 用于选定“选项”的隐藏输入中。
并且图像信息将与其他图像信息一起发送。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-09-29
    • 2014-02-15
    • 2013-10-21
    • 1970-01-01
    • 2021-10-06
    • 2016-07-01
    • 2010-10-19
    • 1970-01-01
    相关资源
    最近更新 更多