Robert
Robert

Reputation: 293

PHP validate form arrays

I have a form with inputs that can be duplicated with jQuery. The inputs are like this

<select name='user_id[]'>
    <option value=''>Select a user</option>
    <option value='1'>Alice</option>
    <option value='2'>Bob</option>
</select>
<input type="text" id="drinks1" name="drinks[]" />
<input type="text" id="drinks2" name="drinks[]" />
<input type="text" id="food1" name="food[]" />
<input type="text" id="food2" name="food[]" />

So in my processing script I have the arrays $_POST['userid'], $_POST['drinks'] and $_POST['food']

How can I check if these are empty? The empty() function doesnt work because they look like this when I print them Array ( [0] => ) which appears to be non-empty.

Upvotes: 1

Views: 2284

Answers (1)

Dennis Hackethal
Dennis Hackethal

Reputation: 14275

Simply loop through the array to check if any field is empty:

foreach($_POST['userid'] as $key=>$value) if(empty($value)) echo "empty";

You can do the same with all your arrays.

Upvotes: 3

Related Questions