Foreach line causing warning

Hi All,

I have this part of script which is ‘getting’ values via a form $_POST (in a foreach loop) :

foreach($_POST['user'] as $key => $username) {
	foreach($_POST['add_user_to_task'][$key] as $check => $checked){
		$task_name = $_POST['task_name'][$key][$check];
		if ($checked == 1) {
		} // end if CHECKED == 1
		else {
	} // end foreach 2
} // end foreach 1

Everything is fine, except when the user doesn’t check ANY checkbox. Then I get the warning

Warning: Invalid argument supplied for foreach() in /path_to_file/ajax/ajaxupload.php on line 14

which is just logic, cause the loop doesn’t get the value for the key (it was NOT checked by the user)
line 14 :

foreach($_POST['add_user_to_task'][$key] as $check => $checked)

I can turn off warning messages by I really want the script to be clean. Now how can I ‘tell’ the second foreach NOT to run if there’s no checked add_user_to_task[$i] value? Any help is appreciated! Regards

if(isset($_POST['add_user_to_task'])) {
// run it

should do it

So simple… THNX :smiley:

Hmmmmm… But, what if @ the first ‘add_user_to_task’ is checked (isset($_POST[‘add_user_to_task’] is TRUE)) and is NOT checked @ the next one? I’m still getting the warning…

Did you copy spikeZ code exactly? I think you’ll need to check if the individual $key is set in that array so, just the same as his snippet but…

if (isset($_POST['add_user_to_task'][$key])) {
// run it

^ yup that would work nicely too.

Thank you guys! I believe I’ll manage to clear those warnings! :slight_smile: