This question already has an answer here:
- Implode array for PHP checkbox in Form 2 answers
I have several checkboxes in a form. And I'm trying to echo those values into an email. Here is the HTML:
<input id="checkbox1" type="checkbox" name="work-check[]"><label for="checkbox2">App Design</label>
<input id="checkbox2" type="checkbox" name="work-check[]"><label for="checkbox1">Design</label>
<input id="checkbox3" type="checkbox" name="work-check[]"><label for="checkbox2">Email Design</label>
<input id="checkbox4" type="checkbox" name="work-check[]"><label for="checkbox1">Layout Design</label>
<input id="checkbox5" type="checkbox" name="work-check[]"><label for="checkbox1">Poster Design</label>
<input id="checkbox6" type="checkbox" name="work-check[]"><label for="checkbox2">Web Design</label>
And here is the PHP:
<?php
if ($_POST["submit"]) {
if(!filter_var($_POST[email], FILTER_VALIDATE_EMAIL)) {
echo '<div data-alert class="alert-box warning">E-mail is not valid</div>';
$error=true;
}
if($error==false) {
$result='<div data-alert class="alert-box success">Form Submitted</div>';
$headers = array("From: myemail@mail.com",
"Reply-To: myemail@mail.com",
"X-Mailer: PHP/" . PHP_VERSION
);
$headers = implode("
", $headers);
// the message
// the message
$msg = "
First Name: ".$_POST['first_name']."
Last Name: ".$_POST['last_name']."
Email: ".$_POST['email']."
Options: ".implode(",", $_POST['work-check']);
// use wordwrap() if lines are longer than 70 characters
$msg = wordwrap($msg,70);
$to = "myemail@mail.com";
$subject = "New Request";
mail($to, $subject, $msg, $headers);
}
}
echo $result;
?>
I think I have it set up that I should get an array back called work-check. I just don't know php well enough to know how to cycle through that array and set the values I get back. I found something like this which I think might be going in the right direction:
// The array will only contain the checked value of forms
foreach($golfer as $g) {
// can use checkbox value here
}
I'm not sure where in the php code that should go.
</div>