bard_the_dragon
bard_the_dragon

Reputation: 33

How do I check if multiple form inputs are empty at once with PHP?

So, I have some code that gets input from an HTML form via POST. I want to check if the input received is blank, but there are multiple form fields and just doing

if(empty($_POST['forminput1'] or $_POST['forminput2'])){

isn't working, it correctly says there's an error if neither of them are filled (there are more fields, but I'm starting with this) but if just one is it says it's correct even if the other one is empty. Does anyone know how I can do this? I think it's because I'm using "OR" but I'm not sure how I'd get an "and/or" sort of thing (kind of new to PHP).

Upvotes: 0

Views: 1814

Answers (2)

Murlidhar Fichadia
Murlidhar Fichadia

Reputation: 2609

Try this :

 $forminput1 = $_POST['forminput1'];

if(!isset($forminput1) || trim($forminput1) == '')
{
echo "You did not fill out the required fields.";
}

This way you can inform user too which exact field is empty or they have not filled.

Upvotes: 0

Dacaspex
Dacaspex

Reputation: 679

You have to type:

if(empty($_POST['forminput1']) || empty($_POST['forminput2'])){

the || means the 'or' operator you are talking about. The empty() function can only handle 1 variable: Reference. It returns TRUE if the variable is empty. If one formfield is empty, that function will return TRUE and your if statement will be TRUE because of the OR operator

Upvotes: 2

Related Questions