【问题标题】:textarea and selector option not being sent to emailtextarea 和选择器选项未发送到电子邮件
【发布时间】:2016-04-14 01:36:47
【问题描述】:

我按照建议进行了搜索,但没有找到我的表单版本不起作用的原因。我有一个表单,它在文本区域输入中收集姓名、电子邮件、电话、选择选项和消息。我根据在选择输入中选择的用户选项更改了 textarea 和 action php。

我使用 PHP 通过电子邮件发送表单内容。我得到所有字段,除了: -留言/评论

HTML

<!-- Career Form -->
<form id="careerContactForm" role="form" action="" method="post" enctype="multipart/form-data">
  <!-- $name -->
  <div class="row form-group">
    <div class="col-md-8 col-md-offset-2 required">
      <label for="contact-name">Full Name</label>
      <input type="text" name="name" class="form-control" id="contact-name" placeholder="Full Name" required>
    </div>
  </div>
  <!-- $email -->
  <div class="row form-group">
    <div class="col-md-4 col-md-offset-2 required">
      <label for="contact-email">Email</label>
      <input type="text" name="email" class="form-control" id="contact-email" placeholder="Email" required>
    </div>
    <!-- $phone -->
    <div class="col-md-4 required">
      <label for="contact-phone">Phone Number</label><br />
      <input type="text" class="form-control bfh-phone" data-country="US" id="contact-phone" name="phone" placeholder="Phone Number" required>
    </div>
  </div>
  <!-- $who -->
  <div class="row form-group">
    <div class="col-md-8 col-md-offset-2 required select-wrapper">
    <!-- Contact -->
      <label for="contact-who">Who are you trying to contact?</label>
      <select class="selectorWho form-control" name="who" required>
        <option value="None"><em>--Please Select One--</em></option>
        <option value="general">General</option>
        <option value="HR / Careers">HR / Careers</option>
        <option value="sales">Sales</option>
        <option value="td">TDXperts</option>
        <option value="Other">Other</option>
      </select>
    </div>
  </div>
  <!-- $interest -->
  <div class="row form-group hidden uploadResume">
    <div class="col-md-8 col-md-offset-2 required select-wrapper">
     <!-- Career -->
      <label>I'm looking for employment opportunities in…</label>
      <select class="selector-career form-control" name="interest" required>
        <option value="None"><em>--Please Select One--</em></option>
        <option value="Accounting">Accounting</option>
        <option value="Administration">Administration</option>
        <option value="Finance">Finance</option>
        <option value="general">General</option>
        <option value="HR">HR</option>
        <option value="IT">IT</option>
        <option value="Logistics & Customs Affairs">Logistics & Customs Affairs</option>
        <option value="Marketing">Marketing</option>
        <option value="Purchasing">Purchasing</option>
        <option value="Sales">Sales</option>
        <option value="Supply Chain Planning">Supply Chain Planning</option>
        <option value="Warehouse">Warehouse</option>
        <option value="Other">Other</option>
      </select>
    </div>
    <div class="col-md-8 col-md-offset-2 required">
      <label for="uploadResume">Upload Your Resume</label>
      <input type="file" name="resume" id="resume-upload">
      <p class="help-block"><em>You must choose a valid file. We accept .doc, .docx, .pdf, .rtf and .txt files</em></p>
    </div>
  </div>
  <!-- Career Submit: hide / show -->
  <div class="row form-group">
    <div class="col-md-8 col-md-offset-2 hidden careerContact">
      <!-- Contact -->
      <label for="contact-message">Questions or Comments</label>
      <textarea name="message" cols="50" rows="6" id="contact-message" class="form-control" placeholder="Would you like to include any more information?" ></textarea>
    </div>
    <div class="col-md-8 col-md-offset-2 hidden contactMessage">
      <!-- Career -->
      <label for="career-message">Message</label>
      <textarea name="comments" cols="50" rows="6" id="career-message" class="form-control" placeholder="Your message..." ></textarea>
    </div>
  </div>
  <div class="row form-group">
    <div class="col-md-8 col-md-offset-2">
      <button type="submit" class="btn">Send message</button>
    </div>
  </div>
</form>

JS(显示/隐藏文本区域和显示/隐藏文件上传对话框)

$(document).ready(function(e) {
$(".selectorWho").on('change', function(e) {
    e.preventDefault();
    var uploadResume = $('.uploadResume');
    var comments = $('.contactMessage');
    var careerComments = $('.careerContact');
    if (this.value == "HR / Careers") {
        uploadResume.slideDown().removeClass("hidden");
        careerComments.removeClass("hidden");
        comments.addClass("hidden");
        var action = "do/careers-submit.php";
        var submitButton = 'career-submit';
    } else {
        uploadResume.slideUp().addClass('hidden');
        careerComments.addClass('hidden');
        comments.removeClass("hidden");
        var action = "do/contact-submit.php";
        var submitButton = 'contact-submit';
    }
    $("#careerContactForm").attr("action", action);
});
});

PHP(用于其中一项操作)

<?php 
    require("../classes/class.phpmailer.php");
    $mail = new PHPMailer();

    $name = $_POST['name'];
    $email = $_POST['email'];
    $phone = $_POST['phone'];
    $who = $_POST['who'];
    $interest = $_POST['interest'];
    $comments = $_POST['comments'];

    $mail->IsSMTP();
    $mail->From     = "$email";
    $mail->FromName = "$name";
    // $mail->AddAddress("hr@tireco.com","Tireco HR");
    $mail->AddAddress("vs@tireco.com","Tireco HR");
    $mail->Subject  = "New Resume Submission";
    $mail->Body     = "Name:\n $name\n\n\nPhone:\n $phone\n\n\nEmail:\n $email\n\n\nContacting:\n $who\n\n\nInterested In:\n -$interest\n\n\nQuestions/Comments:\n $comments";

    if (isset($_FILES['resume']) &&
        $_FILES['resume']['error'] == UPLOAD_ERR_OK) {
        $mail->AddAttachment($_FILES['resume']['tmp_name'],
                         $_FILES['resume']['name']);
    }

    $mail->WordWrap = 50;

    if(!$mail->Send()) {
      echo 'Message was not sent.';
      echo 'Mailer error: ' . $mail->ErrorInfo;
    } else {
        header( 'Location: ../thankYou.html' ) ;
    }
?>

所以,我得到了消息/cmets textarea 文本的所有字段除了

如果您想查看我在电子邮件中收到的内容click here

如果你点击了,你会看到我在提交HR/Careers > IT + Upload File + Message时得到的结果...消息被省略了。

提前感谢您的帮助。

VS

【问题讨论】:

  • 这段代码没有做什么,你需要它做什么?你得到什么样的输出而不是你期望的?是否开启了 PHP 错误?
  • 感谢您的回复。我需要通过电子邮件向我发送姓名、电子邮件、电话、“兴趣”(选择选项)和共同信息/消息的表格。我收到一封只有姓名、电子邮件和电话的邮件。我需要接收带有 cmets 的 textarea 以及他们在选择输入中选择的选项。如何“打开 PHP 错误”?
  • 转到 PHP 目录中的 php.ini 文件。大约 250 到 400 行之后,您会发现一个标有“错误处理和日志记录”的部分。转到“Common Values”选项并取消注释“E_ALL & ~E_NOTICE | E_STRICT”并删除所有以下文本。这将显示除通知之外的所有错误。您必须重新启动才能使其正常工作。

标签: javascript php html forms textarea


【解决方案1】:

您需要为选择框命名,而不是选项。

你有

<select id="interest" class="selector-career form-control" required>

应该是

<select name="interest" id="interest" class="selector-career form-control" required>

关于文件附件,您可能应该检查上传的文件是否正常

if (isset($_FILES['resume']) &&
    $_FILES['resume']['error'] == UPLOAD_ERR_OK) {
    $mail->AddAttachment($_FILES['resume']['tmp_name'],
                         $_FILES['resume']['name']);
}

【讨论】:

  • $message = $_POST['message']; 在我拥有的另一个动作 php 文件中。我根据第一个选择框选择切换操作文件。尽管我有“cmets”代码,但它通过电子邮件发送为“空白”
  • @vs-works 啊好的,所以我写的关于选择框的部分应该可以解决其中的一个问题。关于附件,您可能应该检查文件上传是否正常。我将用更多代码更新我的答案
  • 感谢您提供的代码。但是我没有收到 textarea 消息怎么办?这就是让我困惑的部分。
  • 表单的结束标签在哪里?您能否用我提到的更改更新您的问题并告诉我
  • @vs-works 忘了在我上面的评论中提到你。此外,如果您没有在正确的位置关闭表单,则您的页面上可能还有另一个名称为“cmets”的元素,而它正在使用它。如果页面上的其他地方还有其他表单?
【解决方案2】:

这是我用于我的选择器的东西,我在 div / form 包装器中使用它。
您需要选择选项的索引,而不是选项容器
这是为了选择州,我已经删除了大部分以节省空间。查看它的结构并修改您的代码。
请注意方括号,该方括号为您的客户选择的选项编制索引。 “state_sel”是包含所选项目的变量。我为函数调用省略了按钮。

 <form id="state_opt" name="state_opt">&nbsp;State
<select id="state_mgr" onChange="state_sel=document.state_opt.state_mgr.options[document.state_opt.state_mgr.selectedIndex].value;">
<option selected value="0">None</option>
<option value="AL">Alabama
<option value="MT">Montana
<option value="WI">Wisconsin
<option value="MO">Missouri
<option value="WY">Wyoming
</select>

【讨论】:

  • 感谢您的意见。我使用“名称”属性通过选择器实现了我所需要的。这就是我需要指定的所有内容,以便能够传递用户选择的值并通过我的 PHP 通过电子邮件发送它..
猜你喜欢
  • 2011-09-12
  • 2019-07-19
  • 2016-05-04
  • 1970-01-01
  • 2018-02-18
  • 1970-01-01
  • 1970-01-01
  • 2016-07-18
  • 2011-08-05
相关资源
最近更新 更多