Jump to content

[SOLVED] Send multiple emails based on checkboxes


karenn1

Recommended Posts

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 - [email protected]

Century City - [email protected], 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("[email protected]", "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("[email protected]", "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("[email protected]", "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

 

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(
'[email protected]' => 'Tyger Valley',
'[email protected]' => 'Century City',
'[email protected]' => '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);
}
}
?>

Oh, found out I had a mistake. This should work (using array_search() in stead of key()):

 

<?php
$mails = array(
'[email protected]' => 'Tyger Valley',
'[email protected]' => 'Century City',
'[email protected]' => '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);
}
}
?>

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

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(
'[email protected]' => 'Tyger Valley',
'[email protected]' => 'Century City',
'[email protected]' => '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)?

  • 1 month later...

Hey everyone,

 

I've used the coding above as thebadbad helped me with. This is what it looks like:

 

$mails = array(
'[email protected]' => 'Tyger Valley',	
'[email protected]' => 'Century City',	
'[email protected]' => 'Cape Town CBD',
'[email protected]'  => '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:

 

 

'[email protected]' => 'Tyger Valley',
'[email protected]' => 'Tyger Valley',
'[email protected]' => 'Century City',	
'[email protected]' => 'Cape Town CBD',
'[email protected]'  => 'Southern Suburbs'

 

but the second email doesn't come through. Can anybody please help me with this?

 

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(
'[email protected]' => 'Tyger Valley',
'[email protected]' => 'Tyger Valley',
'[email protected]' => 'Century City',
'[email protected]' => 'Cape Town CBD',
'[email protected]'  => '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);
	}
}
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.