doubi3929 2019-04-07 12:48
浏览 47
已采纳

PHP IF ELSE语句未正确执行

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
".$salutation." ".$name."
Contact Number: ".$contact.".
Event Date: ".$date.".
Type of Event: ".$event.".
Package Selected : ".$subject.".
Event 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

  • 写回答

2条回答 默认 最新

  • dream752614590 2019-04-07 12:55
    关注

    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).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制