Rednaxela Anim Rednaxela Anim - 1 year ago 62
PHP Question

PHP Conditional Statement skips elseif

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 &amp; CASCADE" />

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

$checkbox = $_POST['checkbox'];
$checkboximploded = implode($checkbox);
$toList = array(
'' => 'First Email',
'' => 'Second Email',

if(!empty($_POST['checkbox'])) {
foreach ($checkbox as $value) {

if (preg_match('/EMEA/',$checkboximploded)) {
$toList[""] = "Third Email";
$toList[""] = "Fourth Email";
elseif (preg_match('/NAM/',$checkboximploded)) {
$toList[""] = "Fifth Email";
$toList[""] = "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
statement and never continues to the
statement. What I want to happen is if all 2 checkboxes are checked, it should add all 4 email addresses to the existing

I already spent 8 hours solving the issue but can't find the correct solution. Kindly advise why it seems to be skipping the

Answer Source

Change the elseif to a simple IF From this...

    elseif (preg_match('/NAM/',$checkboximploded)) {

To this...

    if (preg_match('/NAM/',$checkboximploded)) {