Foreach statement returing warning

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Foreach statement returing warning

Post by tecktalkcm0391 »

How come this:

Code: Select all

//line 12 is below:
foreach($_POST['message_checkbox'] as $num => $m_id){
					$sql = "DELETE FROM ".MESSAGES_TABLE." WHERE recipient = '".SESSION_USER."'
							AND folder = 'inbox' AND id = ".$m_id;
					$result = $database->query($sql);
				} // end foreach
keeps giving me this:
Warning: Invalid argument supplied for foreach() in /home/inbox.php on line 12
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Your post variable isn't an array.
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post by tecktalkcm0391 »

thanks didn't catch that..., but now my Javascript isn't working...

Code: Select all

<script language="JavaScript" type="text/javascript">
<!--
function function() {
 	if(document.inbox.mailaction.value=="foward"){
		var checked_total = 0;
		var max = document.inbox.message_checkbox[].length;
		for (var idx = 0; idx < max; idx++) {
		if (eval("document.inbox.message_checkbox[][" + idx + "].checked") == true) {
			checked_total += 1;
		   }
		}
		if(checked_total>1){
			alert("You selected " + checked_total + " messages, you can only foward one message at a time. Please only select one message.");
			return false;
		} else {
			return true;
		}
	}
	
}
-->
</script>
The checkboxes are named "message_checkbox[]"
so how can i get the Javascript to work now?

Note: I changed the JS to the current name of the checkboxes: "message_checkbox[]"
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Have a look through some of the more recent client side threads.
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post by tecktalkcm0391 »

Thanks....

But its still not working my new code is....

Code: Select all

<script language="JavaScript" type="text/javascript"> 
<!-- 
function function() { 
    if(document.inbox.mailaction.value=="foward"){ 
		var checkbox_name = document.getElementsByName('message_checkbox[]');
		var checked_total = 0;
		var max = checkbox_name.length;
		

I AM GETTIN THE PROBLEM 2 LINES AFTER HERE ( THE IF(EVAL...)

		for (var idx = 0; idx < max; idx++) {
			if (eval("" + checkbox_name + "[" + idx + "].checked") == true) {
				checked_total += 1;
			}
		}
		if(checked_total>1){
			alert("You selected " + checked_total + " messages, you can only foward one message at a time. Please only select one message.");
			return false;
		} else {
			return true;
		}
   } 
    
} 
--> 
</script>
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post by tecktalkcm0391 »

Never mind i got it i changed the messed up line to

Code: Select all

if (eval(checkbox_name[idx].checked) == true) {
Post Reply