通过PHP邮件函数发送动态Javascript值

时间:2018-02-27 00:08:56

标签: javascript php html

我创建了一个表单,根据“来宾人数”生成其他字段。 表单正在按照我的意愿运行,但我现在正在努力学习如何从表单中获取这些动态值并通过PHP发送它们。

HTML - 表格

    <div class="contact-form">
  <form action="php/contact-form.php" class="contact-form" id="contactForm" method="post" name="contactform" role="form">
    <div class="form-group has-feedback">
      <label class="sr-only" for="email">Email*</label>
      <input type="email" placeholder="Email" class="form-control" id="email" name="email">
      <i class="fa fa-envelope form-control-feedback"></i>
    </div>
    <div class="form-group has-feedback">
      <label class="sr-only" for="guests">I promise to dance if you play...*</label>
      <textarea type="text" placeholder="I promise to dance if you play..." class="form-control" id="song" name="song"></textarea>
      <i class="fa fa-music form-control-feedback"></i>
    </div>
    <div class="form-group has-feedback">
      <label class="sr-only" for="guests">Other</label>
      <input type="text" placeholder="Other" class="form-control" id="other" name="other">
      <i class="fa fa-question form-control-feedback"></i>

    </div>
    <h4>Number of Guests</h4>
    <select class="form-control" onchange="numofGuests(this)">
      <option>Select number of guests</option>
      <option>1</option>
      <option>2</option>
      <option>3</option>
      <option>4</option>
      <option>5</option>
      <option>6</option>
    </select>

    <div class="form-group has-feedback" id="guestForm"></div>

    <div id="contactFormResponse"></div>

    <input id="cfsubmit" type="submit" value="Send" class="submit-button btn btn-default">
  </form>
</div>

JAVASCRIPT根据“访客人数”选择生成更多字段

    <script type="text/javascript">

        function numofGuests(val) {
    document.getElementById('guestForm').innerHTML = "";
    for (i = 0; i < parseInt(val.value); i++) { 
        document.getElementById('guestFormOne').innerHTML += '<h4>Guest '+(i+1)+'</h4><input type="text" placeholder="Name" class="form-control" id="guest'+(i+1)+'_name" name="guest_name"><div class="col-sm-4"><select class="form-control" id="guest'+(i+1)+'_meal" name="guest_meal"><option>Meal Choice</option> <option>Buffet</option><option>Gluten Free</option><option>Dairy Free</option><option>Vegan</option></select></div><div class="col-sm-4"><select class="form-control" id="guest'+(i+1)+'_age" name="guest_age"><option>Age</option><option>Adult</option><option>Child under 5</option><option>Child 6 - 12</option></select></div><div class="col-sm-4"><input class="form-control" type="text" id="guest'+(i+1)+'_allergy" name="guest_allergy" placeholder="Allergies?"></div>';
    }      
}    

    </script>

PHP发送变量

    $email = $_POST['email'];
    $song = $_POST['song'];
    $other = $_POST['other'];

// Stuck here on how to grab these variables from the 
// Javascript function numofGuests and send them in an email
    $name = $_POST['guest_name'];
    $meal = $_POST['guest_meal'];
    $age = $_POST['guest_age'];
    $allergy = $_POST['guest_allergy'];


    if(isset($_SERVER['HTTP_REFERER'])) {
      $message .= "\n The form is submitted at: ".$_SERVER['HTTP_REFERER'];
   }

    $from = $email;
    $subject = 'Wedding Website - RSVP';

    $body = "From: $email\n 
             Song suggestions: $song\n
             Additional Comments: $other\n
             \n
             --- Guests ---\n
             Name: $name | Meal: $meal | Age: $age | Allergies: $allergy";

我很困惑我将如何让电子邮件$ body显示输入的每位客人的$ name,$ meal,$ age和$ allergy。无论是选择1位客人还是6位客人都这样做。

0 个答案:

没有答案