1

I'm creating a basic form to purchase adult & child tickets from. With the way the form is set up, the user must purchase an adult ticket, but they don't need to purchase a child ticket. I've added some error messages/validations to enforce rules within the form. All of the adult ticket error messages work correctly, but the child ones aren't working right.

I want the child ticket rules to check the following: that a valid number (aka not a letter) has been entered, the quantity is greater than 0, and a whole number has been entered. I thought I had the rules set so they only start validating if the child ticket input is not empty, but instead they're still trying to validate when it is empty, which I don't want it to do considering no child tickets need to be purchased. How do I get this to work correctly?

Here is my PHP code with the error messages.

<?php
  $adult=$_POST['adult'];
  $child=$_POST['child'];
  $date=date('m/d/Y');

  function isInteger($input) {
    return(ctype_digit(strval($input)));
  }
  if (empty($adult)) {
    $error_message='You must purchase at least 1 Adult ticket!';
  }
  else if (!is_numeric($adult)) {
    $error_message="You must enter a valid number!";
  }
  else if ($adult <= 0) {
    $error_message="You must enter a quantity greater than zero!";
  }
  else if (!isInteger($adult)) {
    $error_message="You must enter a whole number for the quantity! (i.e. 1, 2, etc...)";
  }
  else if (!empty(!is_numeric($child))) {
    $error_message="You must enter a valid number!";
  }
  else if (!empty($child <= 0)) {
    $error_message="You must enter a quantity greater than zero!";
  }
  else if (!empty(!isInteger($child))) {
    $error_message="You must enter a whole number for the quantity! (i.e. 1, 2, etc...)";
  }
  else if ($adult + $child > 5) {
    $error_message="Sorry, there is a limit of 5 total tickets per customer!";
  }
 else {
$error_message='';
  }
  if($error_message !=""){
    include('index.php');
    exit();
  }
?>
5
  • 1
    What are you trying to do here !empty($child <= 0) ? !empty and $child <= 0 are separate conditions Commented Feb 21, 2019 at 4:09
  • @mrid My understanding was that that statement (and the similar others) basically meant..."if $child isn't empty and is less than 0" then the error message would execute. I don't want an error message to execute for $child unless it isn't empty because it isn't required to be filled Commented Feb 21, 2019 at 4:11
  • For that you should use isset(). That would tell you if the form has submitted that variable or not Commented Feb 21, 2019 at 4:18
  • Additionally you would want to test your $_POST values for empty or array_key_exists() or isset() or in PHP 7.0+ null coalesce operator$child = $_POST['child'] ?? null to prevent undefined index error, before assigning them to a variable. Commented Feb 21, 2019 at 4:23
  • I don't know what you need but as per your logic, if there's an adult, it wont check for the child since you're checking for child in the else part Commented Feb 21, 2019 at 4:24

2 Answers 2

3

If $child = 1

if(!empty($child <= 0) ) is equivalent to if(!empty(false)) which makes no sense.

Same with (!empty(!is_numeric($child)))

Use if(isset($child) && $child <= 0) {} instead

You can also use $child = isset($_POST['child']) ? $_POST['child'] : 0

Sign up to request clarification or add additional context in comments.

Comments

0

Not looking for an accepted answer, just wanted to suggest a more practical approach.

PHP has some very powerful built-in functions for validation. Two you can use are filter_var and filter_input, which can easily validate numeric values as integers, without needing to check each variation of an integer.

Additionally instead of chaining several elseif conditions, I suggest using throw to immediately raise an Exception that needs to be handled, otherwise halting execution at that point. Accompanied within a try/catch block to handle the exception as desired.

Example https://3v4l.org/PJfLv

$adult = filter_var($_POST['adult'] ?? null, FILTER_VALIDATE_INT);
$child = filter_var($_POST['child'] ?? null, FILTER_VALIDATE_INT);
try {
    if (false === $adult || false === $child) {
        throw new \InvalidArgumentException('You must enter a whole number (1, 2, etc...)');
    }
    if (0 >= $child || 0 >= $adult) {
        throw new \InvalidArgumentException('You must enter a quantity greater than zero!');
    }
    // at this point child and adult must be >= 1

    // ensure total is at least 1 and at most 5
    $total_options = ['options' => ['min_range' => 1, 'max_range' => 5]];
    $total = filter_var($adult + $child, FILTER_VALIDATE_INT, $total_options);
    if (false === $total) {
        throw new \InvalidArgumentException('Sorry, there is a limit of 5 total tickets per customer!');
    }
    // what should happen next...
} catch(\InvalidArgumentException $e) {
    $error_message = $e->getMessage();
    require __DIR__ . '/index.php'; 
    ## always use absolute paths to prevent include_path overrides/overhead
} finally {
  // do something else no regardless of success or Exception...
}

For PHP < 7.0 replace

$_POST['child']) ?? null

with

isset($_POST['child']) ? $_POST['child'] : null

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.