Jump to content

Crippling frustration with foreach()


jackpf

Recommended Posts

Hi all,

As always, this script was working one minute then not the next. It's a delete-multiple-emails script.

 

I'm getting an error- Warning: Invalid argument supplied for foreach() in /home/jackpf/public_html/bin/pages/profile.php on line 565

 

This is my code for the form:

<input type="checkbox" name="email[]" value="'.$id.'" /> //$id being the ID of the email

 

And this is my code for the processing:

<?php
$email = $_POST['email'];

if(count($email) == 0)
{
die('You have not selected any emails.<br /><a href="'.$_SERVER[HTTP_REFERER].'"><span class="c_controls">Back</span></a>');
}
if(!isset($_POST['del']))
{
echo 'Do you really want to delete '.count($email).' email(s)?<br />
<form id="email_delete" action="'.$_SERVER[php_SELF].'?action=profile&status=myprofile&profile=email&email_delete=array" method="post"><div>';
	foreach($email as $key=>$value)
	{
		echo '<input type="hidden" name="email['.$value.']" value="'.$value.'" />';
	}
	echo '<input type="hidden" name="del" value="1" />';
	echo form_submit('email_delete', '<span class="c_controls">Yes</span>').' <a href="'.$_SERVER[HTTP_REFERER].'"><span class="c_controls">No</span>';
echo '</div></form>';
}
?>

 

It seems as though $email isn't an array, but I don't understand why. It worked before!!

 

Any help is greatly appreciated.

 

Thanks, Jack.

Link to comment
https://forums.phpfreaks.com/topic/147450-crippling-frustration-with-foreach/
Share on other sites

Looks like $email is a string for some weird reason.

 

 

The easy solution would be:

 

if(!is_array($email) || count($email) == 0)

 

 

 

But I don't know why it's not an array.....

 

 

Oh wait...

 

 

echo '<input type="hidden" name="email['.$value.']" value="'.$value.'" />'

 

Isn't valid if I remember correctly.  I don't think PHP parses associate arrays like that from form data.  I think you can only use name[] not name[key].

 

 

 

By the way, when ever you have $array[key] that should be $array['key'] if key is a string and not a constant.  (For example $_SERVER[HTTP_REFERER] should be $_SERVER['HTTP_REFERER'] or $_SERVER["HTTP_REFERER"].)

Yeah...I don't understand why.

 

Hmm...Idk why I had email['.$value.'] tbh, it wasn't actually doing anything. But I removed that & it's still not working.

 

And yeah, I should use quotes, I've just got into the habbit of not doing so because of laziness  :D

 

But yeah, it's still not interpreting my email[] as an array for some reason. Any more ideas? ;D

If you look at the generated HTML of the page, does the form look how you would expect it to look?

 

 

 

As for the not using quotes habit, you should break that.  PHP throws a warning every time a constant is not found.  When ever you do arr[key] it checks if key is a constant, and if not, it uses the literal value "key" which will make it throw a warning.

 

 

So, if you do that 10 times in a page, each time there is a page load, PHP logs 10 warnings ;p.

Yeah, I have webdeveloper FF addon, so I checked the actual value of the checkboxes, and they're all fine; with the right ID's and stuff.

 

I actually can't figure it out.

 

And yeah, I will. I've changed them all already :D Thanks for your help btw.

Omg...I found out what it is xD

 

I recently found out that my server had magic quotes enabled, so I did this:

 

if(get_magic_quotes_gpc())
{
$_POST = array_map('stripslashes', $_POST);
}

 

And for some reason this was messing up my arrays. But I've now disabled magic quotes in my .htaccess file instead...so everything should be ok.

 

Just wondering though, if I have this in my root directory .htaccess file, are its effects global? As in, does this apply to all directories?

 

Feel kind of stupid for posting this topic now, considering it was my own stupid function that was messing stuff up :P

 

Thanks for you help though corbin,

Jack.

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.