CKH
CKH

Reputation: 133

Check one of the form input fields is not empty in php

Could you please inform how to check one of the form input fields is not empty in php, no matter what input field names are.

<form name="form1" method="POST">
<input type="text" name="a">
<input type="text" name="b">
</form>

However, please do not inform me the following codes as I know.

 if ($_POST["a"] != "" || $_POST["b"] != "")
 { [proceed....] }

Upvotes: 0

Views: 1324

Answers (2)

Professor Abronsius
Professor Abronsius

Reputation: 33813

Loop through the POST array and test the value like this perhaps

if( $_SERVER['REQUEST_METHOD']=='POST' ){
    foreach( $_POST as $field => $value ){
        if( !empty($value) ){
            exit( $field . 'is NOT empty' );
        }
    }
}

Upvotes: 3

Indrasis Datta
Indrasis Datta

Reputation: 8618

The if statement can get quite long if there are multiple input fields.

You should use array_filter() function on POST data.

Try this:

 if (array_filter($_POST)) {
    echo "Proceed";
 } else {
    echo "One of the fields is blank";
 }

Upvotes: 0

Related Questions