I have an array of checkboxes coded as:
<form action="CascadeFunction.php" method="post" name="cascader" id="cascader">
<input name="checkbox[]" type="checkbox" value="EMEA" /><label for="EMEA">EMEA</label><br />
<input name="checkbox[]" type="checkbox" value="NAM" /><label for="NAM">NAM</label><br />
<input class="btn btn-primary" name="Submit" type="submit" id="submit" value="POST & CASCADE" />
</form>
The following code checks which of those checkboxes are checked. Then by checking the imploded checkbox value, it adds the corresponding email addresses to the $toList
array.
$checkbox = $_POST['checkbox'];
$checkboximploded = implode($checkbox);
$toList = array(
'firstemail@email.com' => 'First Email',
'secondemail@email.com' => 'Second Email',
);
if(!empty($_POST['checkbox'])) {
foreach ($checkbox as $value) {
if (preg_match('/EMEA/',$checkboximploded)) {
$toList["thirdemail@email.com"] = "Third Email";
$toList["fourthemail@email.com"] = "Fourth Email";
}
elseif (preg_match('/NAM/',$checkboximploded)) {
$toList["fifthemail@email.com"] = "Fifth Email";
$toList["sixthemail@email.com"] = "Sixth Email";
}
}
foreach($toList as $email => $name) {
$mail->AddAddress($email, $name);
}
The code works perfectly only if one of the checkboxes is checked. The problem is, if both are checked, it only adds the email addresses found on the 1st if
statement and never continues to the elseif
statement. What I want to happen is if all 2 checkboxes are checked, it should add all 4 email addresses to the existing $toList
array.
I already spent 8 hours solving the issue but can't find the correct solution. Kindly advise why it seems to be skipping the elseif
statement.