2017-04-13 377 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."; 
    }