Jump to content

Check If Array Value Isset


phprocker

Recommended Posts

Hey all.  I have a page that dynamically creates form input fields called "menuitem[]".

 

So on the page that checks the values I need to check if any of those fields were not filed in.  My syntax is wrong any help would be great.

 

test.php

foreach($_POST['menuitem'] as $key => $value)
{
if(!isset($value) || $value = "")
{
	echo "Please fill in all values. You left key " . $key . "empty.";
}
}

 

Nothing gets echoed when I leave the fields empty so I'm doing something wrong here.

Link to comment
https://forums.phpfreaks.com/topic/218610-check-if-array-value-isset/
Share on other sites

Needs to be:

 

if(!isset($value) || $value == "")

 

You used a bad equality operator, common mistake.  I would also use empty($value) as $value will always be set.  Also wrap you foreach in an if(is_array($_POST['menuitem'])) as an unforseen error may pass over a non-array and you will want to account for that, else you'll get a warning looping through it.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.