daniel
daniel

Reputation: 123

Find which $_POST variable is empty for IF statement

Is there a way to find which exact $_POST variable is empty if I am using several variables in one if statement?

Here is a simple example of the if statement:

if(empty($_POST['Type']) || empty($_POST['Company']) || empty($_POST['InvoiceNumber']) || empty($_POST['Price']) || empty($_POST['Date'])) 
{
    header("Location: ".HOST."invoice_final.php?ErrorFillInfo=???");
}

Now what I need to know is if there is some simple solution to know which $_POST variables are empty and then I want replace those variables with "???" and use the GET method to echo exact fields on the next page.

I can do it in a really complicated way where I will separate all five $_POST variables into separate if statements and call the header() function after each if statement but this looks too complicated and therefore I am asking if there is not a better way to check for exact $_POST variables which are empty.

Upvotes: 1

Views: 47

Answers (1)

A l w a y s S u n n y
A l w a y s S u n n y

Reputation: 38502

You can find empty element from array using array_filter() and array_diff()

$non_empty_element = array_filter($_POST);
$empty_element = array_diff($_POST,$non_empty_element);
print_r($empty_element);

DEMO: https://3v4l.org/hXGVU

Based on the comment of OP,

I have on my page 10 inputs fields but only these five fields ($_POST) MUST BE FILLED rest are not MUST HAVE. But then I will get after using array_diff also these inside $empty_element ... Do you understand my point? Is there some way how to filter only these IMPORTANT $_POST THX

DEMO2 https://3v4l.org/KvaB8

Upvotes: 2

Related Questions