1

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.

1
  • Use var_dump on POST, it may help you understand. Commented Sep 11, 2012 at 21:40

1 Answer 1

3

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.

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

1 Comment

I was hoping I wouldn't need to loop through the arrays. I found that !array_filter($array) does the job as well. link

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.