简体   繁体   English

PHP 条件语句跳过 elseif

[英]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.然后通过检查内爆复选框值,将相应的电子邮件地址添加到$toList数组中。

$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.问题是,如果两者都被选中,它只会添加在第一个if语句中找到的电子邮件地址,而不会继续添加到elseif语句中。 What I want to happen is if all 2 checkboxes are checked, it should add all 4 email addresses to the existing $toList array.我想要发生的是如果所有 2 个复选框都被选中,它应该将所有 4 个电子邮件地址添加到现有的$toList数组中。

I already spent 8 hours solving the issue but can't find the correct solution.我已经花了 8 个小时解决这个问题,但找不到正确的解决方案。 Kindly advise why it seems to be skipping the elseif statement.请告知为什么它似乎正在跳过elseif语句。

Change the elseif to a simple IF From this...将 elseif 更改为简单的 IF 从此...

    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).因为你的 else if 正是这样:一个 else if(意思是:如果第一个条件不满足,只检查第二个条件)。

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.如果你想让它成为一个正常的 if(去掉 else),你会告诉程序检查彼此独立的两个条件,根据你的描述,这是你想要的。

Either:任何一个:

  • Change your elseif to an if (and get rid of the loop), or将您的elseif更改为if (并摆脱循环),或
  • Change your preg_match to check against $value更改您的preg_match以检查$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 .如果两个复选框都被勾选,则$_POST['checkbox']array ('EMEA', 'NAM') ,因此$checkboximploded"EMEANAM" ,它匹配ifelseif的正则表达式。 But since if comes first, if wins, regardless of the fact that you're iterating over $checkbox.但是由于if是第一位的, if获胜,无论您是否在$checkbox.迭代$checkbox.

Also, preg_match may be overkill: strpos would suffice in this case.此外, preg_match可能strpos矫枉过正:在这种情况下strpos就足够了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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