以php电子邮件形式获取php数组值

时间:2018-01-21 20:30:41

标签: javascript php html

以下是动态添加字段的html代码,但是我无法在电子邮件中获取array[]值,也在下面添加 php 脚本的脚本我不知道为什么我不能获取无线电或date[]的值。 如果你能看到什么错误,请告诉我。

在下面添加动态表单字段是最大值设置为4的脚本。

var max = 4;
var i = 2;

$(function() {
  var scntDiv = $('#p_scents');

  $('#addScnt').on('click', function() {
    if (i < max + 1) {
      $('<br><p><label class="control-label requiredField" for="date">Date Of Birth<span class="asteriskField">*</span></label><input class="form-control" id="date" name="date" placeholder="MM/DD/YYYY" type="date"/><label class="control-label " for="health">Please Specify if you have or had any Health issue :</label><textarea class="form-control" cols="40" id="health" name="health[]" placeholder="If no write Nill" rows="10"></textarea><label class="control-label ">Smoker</label> <label class="radio-inline"><input name="radio" type="radio" value="Yes"/>Yes</label><label class="radio-inline"><input name="radio" type="radio" value="No"/>No</label><br><label class="control-label ">Gender</label><label class="radio-inline"> &nbsp;<input name="radio" type="radio" value="Male"/>Male</label><label class="radio-inline"><input name="radio" type="radio" value="Female"/>Female</label><br><button><a href="#" class="remScnt">Remove</a></button><p>').appendTo(scntDiv);

      i++;
      $('.remScnt').on('click', function() {
        $(this).parents('p').remove();
        i--;

        return false;

      });
    }
  });
});
<div class="bootstrap-iso">
  <div class="container-fluid">
    <div class="row">
      <div class="col-md-6 col-md-6 col-md-12">
        <form method="post" action="mail.php">
          <div class="form-group">
            <label class="control-label requiredField" for="date">
     Date Of Birth
     <span class="asteriskField">
    *
     </span>
    </label>
            <input class="form-control" id="date" name="date[]" placeholder="MM/DD/YYYY" type="date" />
          </div>
          <div class="form-group ">
            <label class="control-label " for="health">
     Please Specify if you have or had any Health issue :
    </label>
            <textarea class="form-control" cols="40" id="health" name="health[]" placeholder="If no write Nill" rows="10"></textarea>
          </div>
          <div class="form-group ">
            <label class="control-label ">
     Smoker
    </label>
            <div class="">
              <div class="radio">
                <label class="radio">
     <input name="radio[]" type="radio" value="Yes"/>
     Yes
    </label>
              </div>
              <div class="radio">
                <label class="radio">
     <input name="radio[]" type="radio" value="No"/>
     No
    </label>
              </div>
            </div>
          </div>

          <div class="form-group ">
            <label class="control-label ">
     Gender
    </label>
            <div class="">
              <div class="radio">
                <label class="radio-inline">
     <input name="radio1[]" type="radio" value="Male"/>
     Male
    </label>
              </div>
              <div class="radio">
                <label class="radio-inline">
     <input name="radio1[]" type="radio" value="Female"/>
     Female
    </label>
              </div>
            </div>
          </div>

          <button>  <a href="#" id="addScnt">Add Another Person</a>
</button>
          <div id="p_scents">
          </div>

          <div class="form-group ">
            <label class="control-label requiredField" for="doa">
     Date of Arrival
     <span class="asteriskField">
    *
     </span>
    </label>
            <input class="form-control" id="doa" name="doa" placeholder="MM/DD/YYYY" type="text" />
          </div>
          <div class="form-group ">
            <label class="control-label " for="dod">
     Date of Departure
    </label>
            <input class="form-control" id="dod" name="dod" placeholder="MM/DD/YYYY" type="date" />
          </div>
          <div class="form-group ">
            <label class="control-label requiredField" for="name">
     Your Name
     <span class="asteriskField">
    *
     </span>
    </label>
            <input class="form-control" id="name" name="name" type="text" />
          </div>
          <div class="form-group ">
            <label class="control-label " for="tel">
     Telephone #
    </label>
            <input class="form-control" id="tel" name="tel" placeholder="000-000-0000" type="" />
          </div>
          <div class="form-group ">
            <label class="control-label requiredField" for="email">
     Email
     <span class="asteriskField">
    *
     </span>
    </label>
            <input class="form-control" id="email" name="email" type="text" />
          </div>

          <div class="form-group">
            <div>
              <button class="btn btn-primary " name="submit" type="submit">
    Get Quote
     </button>
            </div>
          </div>
        </form>
      </div>
    </div>
  </div>
</div>

下面的PHP代码,用于从上面的表单中获取数组。我试过运行代码无法在多个日期获取值。

PHP:

<?php
$to = "youname@gmail.com";

$health = $_REQUEST['health'] ;
$date = $_REQUEST['date'] ;
$radio = $_REQUEST['radio'] ;

if (isset($_POST['submit']) && $_POST['submit']!= 'none'){ 

if (is_array($_POST['health'])){ 
$health1 = implode(" ", $_POST['health']); // format your array for use 
} else { 
$health1 = $_POST['health']; // no array -> print single value 
} 
if (isset($_POST['date'])) 
{
$date = implode(',', $_POST['date']);

$message = <<<EOL
...
Service: $date

EOL;
}
} 

$message = $_post['message'] ;
$totalmessage = "
Name:       $name \n
Email:      $health1 \n
date:       $date \n   $radio1
Message:             $message \n ";
$headers = "From: $health";

$sent = mail($to, $subject, $totalmessage, $headers);
if($sent)
echo "email sent";
else
print "We encountered an error sending your mail";
?>

1 个答案:

答案 0 :(得分:1)

当您添加其他人的出生日期时,您没有使用数组表示法,因此后面的值会覆盖以前的值。

您需要更改:

...<input class="form-control" id="date" name="date" placeholder="MM/DD/YYYY" type="date"/>...

为:

...<input class="form-control" name="date[]" placeholder="MM/DD/YYYY" type="date"/>...
                                         ^^ here

请注意,我已删除ID,因为ID必须是唯一的。

另请注意,不是在事件处理程序中多次绑定删除按钮,而是可以使用事件委派仅执行一次。

为此,您可以使用它并将其放在另一个点击处理程序之外:

  $('form').on('click', '.remScnt', function() {
  //                     ^^^^^^^^ These are added dynamically
  // ^^^^ This is already on the page on document load
      $(this).parents('p').remove();
      i--;

      return false;

  });