0

I am using php mail() for my contact form. I have a checkbox input with multiple choices. But when a form is submitted, the value in email is 'Array'. Below is the php and html.

Can anyone suggest what's wrong?

HTML

<div class="col-lg-5">
<div class="form-group">
<div style="margin-bottom:-25px;"><label for="form_covers">Additional Covers</label><br />
<input type="checkbox" id="supertopup" name="addcover[]" value="Super Topup"><label for="supertopup"><span style="margin-left:5px;">Super Topup</span>
<input type="checkbox" id="criticalillness" name="addcover[]" value="Critical Illness"><label for="criticalillness"><span style="margin-left:5px;">Critical Illness</span>
<input type="checkbox" id="cancercare" name="addcover[]" value="Cancer Care"><label for="cancercare"><span style="margin-left:5px;">Cancer Care</span>
<input type="checkbox" id="heartcare" name="addcover[]" value="Heart Care"><label for="heartcare"><span style="margin-left:5px;">Heart Care</span></div>
                                                        
<div class="help-block with-errors"></div>
</div>
</div>

PHP

$fields = array('name' => 'Name', 'phone' => 'Phone', 'email' => 'Email', 'hpolicytype' => 'Policy Type', 'addcover' => 'Additional Covers', 'port' => 'Port Existing Policy', 'member1' => '1st Member Name', 'dob1' => '1st Member Date of Birth', 'member2' => '2nd Member Name', 'dob2' => '2nd Member Date of Birth', 'member3' => '3rd Member Name', 'dob3' => '3rd Member Date of Birth', 'member4' => '4th Member Name', 'dob4' => '4th Member Date of Birth');

$emailText = "You have a new message from your contact form\n=============================\n";

foreach ($_POST as $key => $value) {
// If the field exists in the $fields array, include it in the email
if (isset($fields[$key])) {
$emailText .= "$fields[$key]: $value\n";
}
}

Email Snapshot

Form Submit Result

There is one more problem in email that is sent. The date format is incorrect. In the html form the date format is dd-mm-Y whereas the email received has date format Y-mm-dd.

How to change the date format in email?

Thanks & Regards, Pratik

  • isn't it was due to the fact `$_POST['addcover']` is an array? you will need to [`implode`](https://www.php.net/manual/en/function.implode.php) it into something else, like [comma separated](https://stackoverflow.com/questions/2435216/how-do-i-create-a-comma-separated-list-from-an-array-in-php) maybe? depends on how you want to make it looks. – Bagus Tesa Oct 03 '22 at 14:05
  • regarding date format, see [this qa](https://stackoverflow.com/questions/1926542/how-to-format-datetime-most-easily-in-php) hope those helps. anyway, try to keep only one question in a single post. – Bagus Tesa Oct 03 '22 at 14:08
  • Hi, thank you @Bagus Tesa I have been trying to get it work, but unfortunately nothing seems to be working yet. If anyone can suggest how to pass imploded value in above php code that would be helpful. – Pratik Shah Oct 10 '22 at 12:03

0 Answers0