Reputation: 358
I've looked and found several similar questions but can't seem to make the solutions work for me. If someone could point out what I'm missing that would be great.
Basically, I have a form and PHP that checks to see that the fields have been filled out and then validates them. Then I email them. I have everything working except it only emails the last checked value, instead of all of them. I believe my issue is when I add the array to the body for email. Here's my code for that portion:
if (function_exists('htmlspecialchars_decode')) $message[2] = htmlspecialchars_decode($message[2], ENT_QUOTES);
$body = "$name[0]: $name[2]\r\n\r\n";
$body .= "$email[0]: $email[2]\r\n\r\n";
$body .= "$phone[0]: $phone[2]\r\n\r\n";
$body .= "$service: $service\n";
$body .= "$message[0]:\r\n$message[2]\r\n";
if (!$from) $from_value = $email[2];
else $from_value = $from;
require_once('formfiles2/class.phpmailer.php');
$mail = new PHPMailer();
$mail->SetFrom($from_value);
$mail->AddReplyTo($email[2]);
$mail->Subject = "$subject";
$mail->Body = $body;
$mail->AddAddress($to);
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
}
I've tried this as well:
$body .= "Service" .(is_array($_REQUEST['service']) ? implode("\n",$_REQUEST['service']) : $_REQUEST['service']);
Here's that portion of the form:
<form method="post" class="cForm" action="#cform">
.....
<tr>
<td>
<input type="checkbox" name="service" value="sweeping" /> Sweeping<br />
<input type="checkbox" name="service" value="litter" /> Litter Maintenance<br />
<input type="checkbox" name="service" value="snow" /> Snow Removal<br />
<input type="checkbox" name="service" value="wash" /> Power Washing<br />
</td>
<td>
<input type="checkbox" name="service" value="striping" /> Parking Lot Re-Striping<br />
<input type="checkbox" name="service" value="concrete" /> Asphalt/Concrete Work<br />
<input type="checkbox" name="service" value="lawn" /> Lawn Care<br />
</td>
<tr>
....
</form>
So, I think I'm just missing something in my syntax adding the array to the body. Let me know! Thanks in advance for any help!
Upvotes: 3
Views: 3698
Reputation: 1803
or name your checkboxes as an array without indexes: service[]
then you can
if (isset($_POST['service'])) {
foreach ($_POST['service'] as $key => $val) {
// do what you need
}
}
Upvotes: 1
Reputation: 1310
if you have the same name on input type you have to add the name with array, like name="service[]"
Upvotes: 1
Reputation: 14312
Change the checkbox name to an array, e.g.
<input type="checkbox" name="service[]" value="sweeping" /> Sweeping
Upvotes: 1
Reputation: 145482
You cannot have multiple checkboxes with the same name. That will behave like select options. Multiple checked values will just overwrite each other.
You should adapt your entries to something like:
<input type="checkbox" name="service[stripping]" value="striping" /> Parking Lot Re-Striping<br />
<input type="checkbox" name="service[concrete]" value="concrete" /> Asphalt/Concrete Work<br />
<input type="checkbox" name="service[lawn]" value="lawn" /> Lawn Care<br />
(Doesn't need to be associative. A simple services[]
would already do. Just seems nicer and more explicit.)
Then then combine the incoming array into a text list:
$service = join(", ", $_REQUEST["service"]);
Upvotes: 4