简体   繁体   中英

PHP IF ELSE statement not executing properly

I have a html form and a php script processing the input of the form for validation purposes but im facing an issue. if the form is empty, it will not execute the first header in the if statement which is header("Location: error.html?mailerror"); instead it straight away execute the header in the elseif statement which is header("Location: thanks.html?mailsend"); . I wrote this php validation script to ensure that users do not submit an empty form but it seems like it wont work. Really appreciate if you could help me out.

I have tried putting an echo in the first if statement and it executes properly for example, when a user submit an empty form, it would echo 'Please fill up all fields' Now I'm trying to put a header that points to an error.html document. That is why i replaced the echo to header just like the one is the else if statement with the only difference that it points to the error.html page.

<?php
 if(isset($_POST['submit'])){
     $salutation=$_POST['salutation'];
     $name=$_POST['name'];
     $subject=$_POST['subject'];
     $contact=$_POST['contact'];
     $mailFrom=$_POST['mail'];
     $date=$_POST['date'];
     $event=$_POST['event'];
     $address=$_POST['address'];

 /* If form IS empty it will execute the code below */     
   if(empty($salutation)||empty($name)||empty($subject)||empty($contact)||empty($mail)||empty($date)||empty($event)||empty($address)){
        header("Location: error.html?mailerror");
     }

/* If the form is NOT empty it will execute the code below */    
 elseif(!empty($salutation)||empty($name)||empty($subject)||empty($contact)||empty($mail)||empty($date)||empty($event)||empty($address))

     $mailTo="test@fairus-test-site.webstarterz.com";
     $headers="From: ".$mailFrom;
     $txt = "You have receive a booking from\n".$salutation." ".$name."\nContact Number: ".$contact.".\nEvent Date: ".$date.".\nType of Event: ".$event.".\nPackage Selected : ".$subject.".\nEvent Address: ".$address;

     mail($mailTo,$subject,$txt,$headers);
     header("Location: thanks.html?mailsend");

 }

Once user click submit, it will execute the second if else statement even when the form is empty

Exit the header:

header("Location: error.html?mailerror");
exit;

Otherwise, the script will continue to run. And since you don't have brackets {} around the second if() , it will run every statement (after the statement immediately after the if() statement).

I think this because you are try to check first !empty line in elseif, this mean.. if $salutation is not empty and name is empty .. etc. so, there are wrong if else methode.

elseif(!empty($salutation)||!empty($name)||!empty($subject)||!empty($contact)||!empty($mail)||!empty($date)||!empty($event)||!empty($address)){
 // your code
}

and i didn't see {} in else if on your code.

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