3

I need to check empty input when post form data. I Have this Code :

foreach(array_filter($_POST['video']) as $video_url){   
    if (!empty($video_url)) {

    echo 'true';

    }
        else
    {

    echo 'false';

    }
}

But this code not work for empty input. I print_r($_POST['video']) when send empty input(before foreach code) and see this result:

Array ( [0] => ) 

how do check empty input in my case?!

3
  • if ($video_url != "") try this Commented Aug 26, 2015 at 11:07
  • @syedmohamed: for not empty work true and show true message but for empty input not work and not show any result. Commented Aug 26, 2015 at 11:10
  • if(isset($_POST['video']) && $_POST['video'] !="") Out side foreach Commented Aug 26, 2015 at 11:13

4 Answers 4

3

Your array_filter is making the array empty and you can't iterate over an empty array.

You should use:

$video = array_filter($_POST['video']);

if (empty($video))
{
  echo 'false';
}
Sign up to request clarification or add additional context in comments.

Comments

2

Use this:

// removes all NULL, FALSE and Empty Strings but leaves 0 (zero) values
$result = array_filter( $array, 'strlen' );

Taken from the PHP.net website.

Comments

2

Try this

 if (sizeof($video_url)!=0) {

    echo 'true';

    }
        else
    {

    echo 'false';

    }

Comments

0

You can use string functions. Check this out;

foreach(array_filter($_POST['video']) as $video_url)
{
    if (strlen(str_replace(' ','',$video_url))==0)
    {
        return false;
    }
    else
    {
        return true;
    }
}

Comments

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.