pamfromoz
pamfromoz

Reputation: 7

If else statement

Trying to add a 4th else statement but it is not working. Can't find an example online that works. First 3 statements work, but not the 4th. Should it be If, elseif, elseif, else or If, elseif, else, 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;
     }

Upvotes: 0

Views: 77

Answers (6)

pamfromoz
pamfromoz

Reputation: 7

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.";
    }

Upvotes: 0

Tony
Tony

Reputation: 3058

$fromName == You can't join an else to as an else. Change it to an else if. For example:

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.";
}

Upvotes: 0

Edwin Barahona
Edwin Barahona

Reputation: 189

There is no if for your last else statement. It should look similar to this:

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

Upvotes: 1

Thyrst
Thyrst

Reputation: 2568

Maybe you are trying to do this:

if (condition1) {
    do_this;
} else {
    if (condition2) {
        do_that;
    } else {
        do_something_else;
    }
}

Upvotes: 0

Hossain Azad
Hossain Azad

Reputation: 62

This is not the proper way to write if....elseif...else statement. You should write it following :

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

There you can use any number of elseif condition using same process .

Upvotes: 0

Sehdev
Sehdev

Reputation: 5662

You cannot use else twice. Change second last else to elseif as below:

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;
     }

Upvotes: 0

Related Questions