0

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;
    }
5
  • 1
    You need to group the conditions.. probably you need if ($service == "airTransfer" AND ( .. rest conditions .. )) { and ` if ($service == "hourly" AND ( .. rest conditions .. )) {` Commented Oct 5, 2014 at 2:20
  • Cheery, I believe that is what I have in the code. Maybe I am misunderstanding your suggested syntax? Commented Oct 5, 2014 at 2:23
  • 1
    Nope, this is not what you have in code. AND is applied only between $service and $occasions, the rest goes with OR in between. Commented Oct 5, 2014 at 2:24
  • I wrote if ($service == "airTransfer" AND ($airline == "" OR $flight == "" OR $landing == "")) {echo "Please enter your flight information."; exit;} Commented Oct 5, 2014 at 2:30
  • Awesome, that makes sense. I also did not see the parenthesis after "AND" and after the last condition. Thanks Cheery! Commented Oct 5, 2014 at 2:40

1 Answer 1

1

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
    }
  } 
?>
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.