Jump to content

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 - 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

 

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);
}
}
?>

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);
}
}
?>

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(
'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)?

  • 1 month later...

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?

 

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);
	}
}
}
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.