PHP"如果"声明问题

时间:2014-10-05 02:12:52

标签: php if-statement

我在使if语句正常工作时遇到了一些麻烦。页面上有一个下拉菜单,有两个选项。如果用户选择一个选项,则会显示更多字段,如果他们选择另一个选项,则会显示一组不同的字段。所以,我需要这些字段是强制性的。此代码执行此操作,但是如果用户选择“airTransfer”选项,则仍然需要“每小时”字段,用户甚至无法看到这些字段。

PHP

//Requiring Airline Fields
    if ($service == "airTransfer" AND
        $airline == "" OR
        $flight == "" OR
        $landing == "") {
        echo "Please enter your flight information.";
        exit;
    }


//Requiring Hourly Fields
    if ($service == "hourly" AND
        $occasion == "" OR
        $start == "" OR
        $end == "" OR
        $pickup == "" OR
        $hours == "") {
        echo "Please enter all event details.";
        exit;
    }

1 个答案:

答案 0 :(得分:1)

请尝试此代码

<?php

if($service == "airTransfer") //Requiring Airline Fields
{
  if($airline == "" || $flight == "" || $landing == "")
  {
    echo "Please enter your flight information.";
    exit;
  }
  else
  {
    // do something 
  }
} // End Requiring Airline Fields
elseif($service == "hourly") //Requiring Hourly Fields
{
  if($occasion == "" || $start == "" || $end == "" || $pickup == "" || $hours == "")
  {
    echo "Please enter all event details.";
    exit;
  }
  else
  {
    // do somthing
  }
} // end Requiring Hourly Fields

?>

如果您愿意,也可以使用empty()

<?php
  if($service == "airTransfer") //Requiring Airline Fields
  {
    if(empty($airline) || empty($flight) || empty($landing))
    {
      echo "Please enter your flight information.";
      exit;
    }
    else
    {
      // do something 
    }
  } 
  elseif($service == "hourly") //Requiring Hourly Fields
  {
    if(empty($occasion) || empty($start) || empty($end) || empty($pickup) || empty($hours))
    {
      echo "Please enter all event details.";
      exit;
    }
    else
    {
      // do somthing
    }
  } 
?>