karenn1 Posted June 9, 2008 Share Posted June 9, 2008 Hi guys, I have a form on an HTML page with three checkboxes like this: <input name="area" type="checkbox" id="area" value="Tyger Valley">Tyger Valley<br> <input name="area" type="checkbox" id="area" value="Century City">Century City<br> <input name="area" type="checkbox" id="area" value="Cape Town CBD">Cape Town CBD<br> The idea is for a user to select one or more areas of interest and then a mail gets sent to the applicable parties: ie. Tygervalley - tygervalley@mysite.com Century City - cc@mysite.com, etc My PHP code looks like this: if ($_POST['area'] == "Tyger Valley"){ $headers2 = "MIME-Version: 1.0" . "\r\n"; $headers2 .= "Content-type: text/html; charset=iso-8859-1" . "\r\n"; mail("tygervalley@mysite.com", "Cosmoprop :: Small Serviced Offices :: Submission Confirmation :: Tyger Valley", $msg2, $headers); } if ($_POST['area'] == "Century City"){ $headers2 = "MIME-Version: 1.0" . "\r\n"; $headers2 .= "Content-type: text/html; charset=iso-8859-1" . "\r\n"; mail("cc@mysite.com", "Cosmoprop :: Small Serviced Offices :: Submission Confirmation :: Tyger Valley", $msg2, $headers); } if ($_POST['area'] == "Cape Town CBD"){ $headers2 = "MIME-Version: 1.0" . "\r\n"; $headers2 .= "Content-type: text/html; charset=iso-8859-1" . "\r\n"; mail("capetown@mysite.com", "Cosmoprop :: Small Serviced Offices :: Submission Confirmation :: Tyger Valley", $msg2, $headers); } When selecting one checkbox, it sends the email fine but if I select more than one, it only sends the email to the last checked box. How can I set it that the code reads all three boxes and send to all three addresses if they are all selected? Thanks! Karen Quote Link to comment https://forums.phpfreaks.com/topic/109366-solved-send-multiple-emails-based-on-checkboxes/ Share on other sites More sharing options...
Guest Xanza Posted June 9, 2008 Share Posted June 9, 2008 Use a loop to check if each one exists. If it does, include it in the mail. Quote Link to comment https://forums.phpfreaks.com/topic/109366-solved-send-multiple-emails-based-on-checkboxes/#findComment-560960 Share on other sites More sharing options...
thebadbad Posted June 9, 2008 Share Posted June 9, 2008 You should put a set of square brackets at the end of the checkboxes names, in order to get the checked values in an array (else you'll only get the last checked value): <input name="area[]" type="checkbox" id="area" value="Tyger Valley">Tyger Valley<br> <input name="area[]" type="checkbox" id="area" value="Century City">Century City<br> <input name="area[]" type="checkbox" id="area" value="Cape Town CBD">Cape Town CBD<br> Then loop through the array, and send the mails: <?php $mails = array( 'tygervalley@mysite.com' => 'Tyger Valley', 'cc@mysite.com' => 'Century City', 'capetown@mysite.com' => 'Cape Town CBD' ); foreach ($_POST['area'] as $value) { //if the checkbox value is found in the array above, send a mail to the corresponding address if (in_array($value, $mails)) { mail(key($_POST['area']), "Cosmoprop :: Small Serviced Offices :: Submission Confirmation :: $value", $msg2, $headers); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/109366-solved-send-multiple-emails-based-on-checkboxes/#findComment-560967 Share on other sites More sharing options...
karenn1 Posted June 9, 2008 Author Share Posted June 9, 2008 thebadbad, Thanks for your reply. I appreciate it! I copied in your coding but it's not really working. When I fill out the form, no emails come through after selecting two checkboxes. Any ideas? Karen Quote Link to comment https://forums.phpfreaks.com/topic/109366-solved-send-multiple-emails-based-on-checkboxes/#findComment-561031 Share on other sites More sharing options...
thebadbad Posted June 9, 2008 Share Posted June 9, 2008 Oh, found out I had a mistake. This should work (using array_search() in stead of key()): <?php $mails = array( 'tygervalley@mysite.com' => 'Tyger Valley', 'cc@mysite.com' => 'Century City', 'capetown@mysite.com' => 'Cape Town CBD' ); foreach ($_POST['area'] as $value) { //if the checkbox value is found in the array above, send a mail to the corresponding address if (in_array($value, $mails)) { mail(array_search($value, $mails), "Cosmoprop :: Small Serviced Offices :: Submission Confirmation :: $value", $msg2, $headers); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/109366-solved-send-multiple-emails-based-on-checkboxes/#findComment-561052 Share on other sites More sharing options...
karenn1 Posted June 9, 2008 Author Share Posted June 9, 2008 It's working 100% now thanks. Another question, in the email that's sent, I used $_POST['area'] to get the area name in the email. This now returns "Array". How can I get the actual value? I tried $value but that didn't work. Comes out blank. Thanks! Karen Quote Link to comment https://forums.phpfreaks.com/topic/109366-solved-send-multiple-emails-based-on-checkboxes/#findComment-561077 Share on other sites More sharing options...
thebadbad Posted June 9, 2008 Share Posted June 9, 2008 Great. Where do you want to access the name? In the actual message? 'Cause then you'll need to move the definition of the message inside the foreach loop. There, $value does return the name. Like: <?php $mails = array( 'tygervalley@mysite.com' => 'Tyger Valley', 'cc@mysite.com' => 'Century City', 'capetown@mysite.com' => 'Cape Town CBD' ); foreach ($_POST['area'] as $value) { //if the checkbox value is found in the array above, send a mail to the corresponding address if (in_array($value, $mails)) { //define message variable $msg2 = "This is the email body. Here goes the area name: $value."; mail(array_search($value, $mails), "Cosmoprop :: Small Serviced Offices :: Submission Confirmation :: $value", $msg2, $headers); } } ?> You send the same message to each address, right (except for the instances of the area name, of course)? Quote Link to comment https://forums.phpfreaks.com/topic/109366-solved-send-multiple-emails-based-on-checkboxes/#findComment-561083 Share on other sites More sharing options...
karenn1 Posted July 10, 2008 Author Share Posted July 10, 2008 Hey everyone, I've used the coding above as thebadbad helped me with. This is what it looks like: $mails = array( 'tv@mysite.com' => 'Tyger Valley', 'cc@mysite.com' => 'Century City', 'cbd@mysite.com' => 'Cape Town CBD', 'ss@mysite.com' => 'Southern Suburbs' ); foreach ($_POST['area'] as $value) { //if the checkbox value is found in the array above, send a mail to the corresponding address if (in_array($value, $mails)) { mail(array_search($value, $mails), "Small Serviced Offices :: Enquiry for $value", $msg2, $headers); } } This works 100%. My only question, is how can I have two email addresses to go to Tyger Valley? I've tried the following: 'tv@mysite.com' => 'Tyger Valley', 'tv2@mysite.com' => 'Tyger Valley', 'cc@mysite.com' => 'Century City', 'cbd@mysite.com' => 'Cape Town CBD', 'ss@mysite.com' => 'Southern Suburbs' but the second email doesn't come through. Can anybody please help me with this? Quote Link to comment https://forums.phpfreaks.com/topic/109366-solved-send-multiple-emails-based-on-checkboxes/#findComment-586185 Share on other sites More sharing options...
thebadbad Posted July 10, 2008 Share Posted July 10, 2008 Hi again. It's not working because array_search() only returns the first key (mail address) to the matching searched value (name). To return multiple mails matching the same name, use array_keys(): <?php $mails = array( 'tv@mysite.com' => 'Tyger Valley', 'tv2@mysite.com' => 'Tyger Valley', 'cc@mysite.com' => 'Century City', 'cbd@mysite.com' => 'Cape Town CBD', 'ss@mysite.com' => 'Southern Suburbs' ); foreach ($_POST['area'] as $value) { //if the checkbox value is found in the array above, send a mail to the corresponding address if (in_array($value, $mails)) { //retrieve one or more addresses matching the found value, from the mails array $addrs = array_keys($mails, $value); //send mail to every address found above foreach ($addrs as $addr) { mail($addr, "Small Serviced Offices :: Enquiry for $value", $msg2, $headers); } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/109366-solved-send-multiple-emails-based-on-checkboxes/#findComment-586210 Share on other sites More sharing options...
karenn1 Posted July 10, 2008 Author Share Posted July 10, 2008 Hi badbad, Thanks! It works 100%. You're a star!! Karen Quote Link to comment https://forums.phpfreaks.com/topic/109366-solved-send-multiple-emails-based-on-checkboxes/#findComment-586253 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.