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

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)