Blue
Blue

Reputation: 41

PHP "if" Statement Issue

I am having a bit of trouble getting this if statement to work correctly. There is a drop down on the page with two options. If the user selects one option, a few more fields appear and if they select the other option, a different set of fields appear. So, I need those fields to be mandatory. This code does that, however if the user selects option "airTransfer" it is still requiring the fields for "hourly", which the user cannot even see.

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

Upvotes: 0

Views: 57

Answers (1)

SULTAN
SULTAN

Reputation: 1159

Please try this code

<?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

?>

also you can user the empty() if you wish

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

Upvotes: 1

Related Questions