2017-04-13 317 views
0

尝试添加第4个else语句,但它不起作用。无法在网上找到适用的示例。前3个声明有效,但不是第4声明。应该是If,elseif,elseif,else还是if,elseif,else,else?If else语句

if(empty($fromName) or empty($fromEmail) or empty($subject) or empty($comments)) { 
    echo 'You cannot submit the form with empty fields. Please correct the form and resubmit.'; 
    return false; 
} 
elseif($fieldDelete == "Delete this text!"){  
    $msg ="Delete the contents of the fourth field before submitting."; 
    print $msg; 
} 
else { 
    $flgchk = mail ("$to", "$subject", "$message", "$headers"); 
    $imgfile = "images/NatMap logo2.gif"; 
    $handle = fopen($filename, "r"); 
    $imgbinary = fread(fopen($imgfile, "r"), filesize($imgfile)); 
    echo '<img src="data:image/gif;base64,' . base64_encode($imgbinary) . '" width=427 height=72 />'; 
    echo "\n<br />\n<br />Thank You! An e-mail has been sent to the National Map web team and they will get back to you in the next 24-48 hours."; 
} 

else ($fieldDelete == "Delete this text!"){ 
    $msg ="Delete the contents of the fourth field before submitting."; 
    print $msg; 
    } 
+2

显然,除非你把第三个成'elseif'你不能添加第四个'if'。阅读你已经编写的代码。 –

+0

每个'if'只能有一个'else'语句。如果没有'if'或'elseif'测试匹配,则使用它。另一个是什么意思? – Barmar

+0

你不能在'else'之后有条件。它必须是'elseif'。它需要在“其他”之前。 – Barmar

回答

0

$ FROMNAME ==您不能加入到elseelse。将其更改为else if。例如:

if(empty($fromName) or empty($fromEmail) or empty($subject) or empty($comments)) { 
    echo 'You cannot submit the form with empty fields. Please correct the form and resubmit.'; 
    return false; 

}elseif($fieldDelete == "Delete this text!"){  
    $msg ="Delete the contents of the fourth field before submitting."; 
    print $msg; 

}elseif($fromName == "Curtisvien" || $fromName == "RichardMark" || $fromName == "Thomastymn"){ 

    $msg ="You are blocked."; print $msg; 

}else{ 
    $flgchk = mail ("$to", "$subject", "$message", "$headers"); 
    $imgfile = "images/NatMap logo2.gif"; 
    $handle = fopen($filename, "r"); 
    $imgbinary = fread(fopen($imgfile, "r"), filesize($imgfile)); 
    echo '<img src="data:image/gif;base64,' . base64_encode($imgbinary) . '" width=427 height=72 />'; 
    echo "\n<br />\n<br />Thank You! An e-mail has been sent to the National Map web team and they will get back to you in the next 24-48 hours."; 
} 
+0

这一个工作!非常感谢你。 – pamfromoz

+0

@pamfromoz如果这个答案解决了你的问题,你可以接受它来解决问题。 – Tony

+0

等待,仍然在打嗝。这就是我现在所拥有的: – pamfromoz

0

您不能使用其他两次。更改倒数第二别的如下ELSEIF:

if(empty($fromName) or empty($fromEmail) or empty($subject) or empty($comments)) { 
    echo 'You cannot submit the form with empty fields. Please correct the form and resubmit.'; 
    return false; 
} 
elseif($fieldDelete == "Delete this text!"){  
    $msg ="Delete the contents of the fourth field before submitting."; 
    print $msg; 
} 
elseif { 
    $flgchk = mail ("$to", "$subject", "$message", "$headers"); 
    $imgfile = "images/NatMap logo2.gif"; 
    $handle = fopen($filename, "r"); 
    $imgbinary = fread(fopen($imgfile, "r"), filesize($imgfile)); 
    echo '<img src="data:image/gif;base64,' . base64_encode($imgbinary) . '" width=427 height=72 />'; 
    echo "\n<br />\n<br />Thank You! An e-mail has been sent to the National Map web team and they will get back to you in the next 24-48 hours."; 
} 

else ($fieldDelete == "Delete this text!"){ 
    $msg ="Delete the contents of the fourth field before submitting."; 
    print $msg; 
    } 
1

没有if你最后else声明。它应该看起来类似于此:

if (condition1) { 
    /* ... */ 
} 
elseif (condition2) { 
    /* ... */ 
} 
elseif (condition3) { 
    /* ... */ 
} 
else { 
    /* ... */ 
} 
0

这不是正确的方式来编写if .... elseif ... else语句。 你应该写如下:

if() 
{ 
    //statement; 
}elseif() 
{ 
    //statement; 
}elseif() 
{ 
    if() 
    { 
    //Nested Statement; 
    }else{ 
    //Nested Statement; 
    } 
}else{ 
    //If all of above is false then this will execute ; 
} 

在那里,你可以使用任意数量的使用相同的工艺条件ELSEIF的。

0

也许你正在试图做到这一点:

if (condition1) { 
    do_this; 
} else { 
    if (condition2) { 
     do_that; 
    } else { 
     do_something_else; 
    } 
} 
0
if(empty($fromName) or empty($fromEmail) or empty($subject) or empty($comments)) { 
    echo 'You cannot submit the form with empty fields. Please correct the form and resubmit.'; 
    return false; 
} 
elseif($fieldDelete == "Delete this text!"){  
     $msg ="Delete the contents of the fourth field before submitting."; 
     print $msg; 
    } 
elseif (($fromName == "Curtisvien") || ($fromName == "Thomastymn") || ($fromName == "RichardMark")) { 
     $msg ="You are blocked."; 
     print $msg; 
} 

    else { 
      $flgchk = mail ("$to", "$subject", "$message", "$headers"); 
      $imgfile = "images/NatMap logo2.gif"; 
      $handle = fopen($filename, "r"); 
      $imgbinary = fread(fopen($imgfile, "r"), filesize($imgfile)); 
      echo '<img src="data:image/gif;base64,' . base64_encode($imgbinary) . '" width=427 height=72 />'; 
      echo "\n<br />\n<br />Thank You! An e-mail has been sent to the National Map web team and they will get back to you in the next 24-48 hours."; 
    }