简体   繁体   中英

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" />
</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.

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

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

To this...

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

Because your else if is exactly that: an else if (meaning: only check the second condition if the first is not met).

If you would make that a normal if (drop the else) you would tell the program to check for both conditions independent from each other, which is what you want based on your description.

Either:

  • Change your elseif to an if (and get rid of the loop), or
  • Change your preg_match to check against $value

Here's why. If both check boxes are ticked, then $_POST['checkbox'] is array ('EMEA', 'NAM') and thus $checkboximploded is "EMEANAM" , which matches the regular expression of the if as well as the elseif . But since if comes first, if wins, regardless of the fact that you're iterating over $checkbox.

Also, preg_match may be overkill: strpos would suffice in this case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM