rsethi
rsethi

Reputation: 35

Getting php array values in php email form

Here is html code with dynamically added fields however I am not able to get array[] values in email ,also adding script for php script below I am not sure why I am not able to get values for radio or date[]. please let me know if you can see whats wrong.

Adding dynamic form fields below is script with max value set to 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 code below for getting array from form above. I tried running the code could not get the values in multiple date.

PHP:

<?php
$to = "[email protected]";

$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";
?>

Upvotes: 0

Views: 128

Answers (1)

jeroen
jeroen

Reputation: 91744

When you add the date of birth of an additional person, you are not using the array notation, so the values of the later ones will overwrite the previous ones.

You need to change:

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

to:

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

Note that I have removed the ID as id's need to be unique.

Also note that instead of binding the remove button multiple times in the event handler, you can use event delegation to do that only once.

To do that, you can use this and put it outside of the other click handler:

  $('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;

  });

Upvotes: 1

Related Questions