NathanS Posted March 12, 2008 Share Posted March 12, 2008 hi guys, i have a form, which the user selects multiple entries using a tickbox. when the form is submitted, it will email each ticked person (well, that's the theory!!) I have tried using foreach in the below way, yet it only ever emails the first person. Any ideas on this? Many thanks foreach ($email as $e) { if ($status == 'Pending') { if ($sourceofbusiness == 'BusinessHere') { $msg = "Message Here"; $recipient = $e; $subject = "Email"; $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; $headers .= "From: Company <[email protected]> \n"; mail($recipient, $subject, $msg, $headers); } (code continues in that way, dependednt on status/source of business) Link to comment https://forums.phpfreaks.com/topic/95805-help-with-foreach-or-similar-function/ Share on other sites More sharing options...
micah1701 Posted March 12, 2008 Share Posted March 12, 2008 in your html form set the name of the checkbox input to name="email[]" that way it will create an array of the e-mail addresses and the foreach loop should work Link to comment https://forums.phpfreaks.com/topic/95805-help-with-foreach-or-similar-function/#findComment-490463 Share on other sites More sharing options...
NathanS Posted March 12, 2008 Author Share Posted March 12, 2008 Thanks for the quick reply. I've already done that as follows: echo "<INPUT TYPE='checkbox' name='EMAIL[]' VALUE='$field20' style='visibility:hidden' checked>"; And in the php file, I have the variables declared as follows: $email = $_POST["EMAIL"]; Then I use the foreach loop before the email code - However, it only sends an email to one address. Thanks Link to comment https://forums.phpfreaks.com/topic/95805-help-with-foreach-or-similar-function/#findComment-490469 Share on other sites More sharing options...
craygo Posted March 12, 2008 Share Posted March 12, 2008 if you name the checkboxes email[] and you are using POST variables then the foreach loop would look like this <?php foreach($_POST['email'] as $e){ if ($sourceofbusiness == 'BusinessHere') { $msg = "Message Here"; $recipient = $e; $subject = "Email"; $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; $headers .= "From: Company <[email protected]> \n"; mail($recipient, $subject, $msg, $headers); } } ?> Remember globals are not on by default so you just can't use $email you would have to call it as $_POST['email Also if the message is going to be the same why send out the mail a bunch of times, put the recipients in a blind copy and send it out that way <?php $bcc = ""; $count = 0; foreach($_POST['email'] as $e){ $bcc .= $e; $count++; if ($count < $num_rows) { $bcc .= ", "; } } if ($sourceofbusiness == 'BusinessHere') { $msg = "Message Here"; $recipient = ""; $subject = "Email"; $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; $headers .= "From: Company <[email protected]> \n"; $headers .= "BCC: $bcc\r\n"; mail($recipient, $subject, $msg, $headers); } ?> Ray Link to comment https://forums.phpfreaks.com/topic/95805-help-with-foreach-or-similar-function/#findComment-490473 Share on other sites More sharing options...
NathanS Posted March 12, 2008 Author Share Posted March 12, 2008 Thanks for the reply Using your method, calling _POST['EMAIL'], still only sends an email to the first address. Unsure what I'm doing wrong here Link to comment https://forums.phpfreaks.com/topic/95805-help-with-foreach-or-similar-function/#findComment-490495 Share on other sites More sharing options...
craygo Posted March 12, 2008 Share Posted March 12, 2008 try and echo out the post variables print_r($_POST); And let us know what you get Ray Link to comment https://forums.phpfreaks.com/topic/95805-help-with-foreach-or-similar-function/#findComment-490497 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.