proggR Posted March 27, 2009 Share Posted March 27, 2009 $VAR = $_GET['topping']; foreach($VAR as $key ){ echo $VAR; echo $key; } Warning: Invalid argument supplied for foreach() in /var/www/order.php on line 25 Could someone tell me whats wrong with that code? Why I'm getting the error I'm getting I'm stumped. I want to be able to say if($key!=null) increment counter or something to that effect. Why is this not working? topping is a bunch of textboxes. I just want to see if they're checked or not. Anyway. The main concern is this code doesn't work and I can't figure out why not. Thanks in advance. Link to comment https://forums.phpfreaks.com/topic/151346-foreach-from-_get/ Share on other sites More sharing options...
corbin Posted March 27, 2009 Share Posted March 27, 2009 $VAR is not an array, according to the error. Do var_dump($VAR) and see what's in it. Should VAR be an array? Link to comment https://forums.phpfreaks.com/topic/151346-foreach-from-_get/#findComment-794906 Share on other sites More sharing options...
suma237 Posted March 27, 2009 Share Posted March 27, 2009 $var should be an array $VAR[] = "example"; foreach($VAR as $key ){ echo $VAR; echo $key; } [code] Link to comment https://forums.phpfreaks.com/topic/151346-foreach-from-_get/#findComment-794908 Share on other sites More sharing options...
Daniel0 Posted March 27, 2009 Share Posted March 27, 2009 $_GET['toppings'] will not be set if there are none of your toppings checkboxes that are checked. Try type casting it as an array or use a conditional control structure to avoid the loop altogether if it is empty. Link to comment https://forums.phpfreaks.com/topic/151346-foreach-from-_get/#findComment-794928 Share on other sites More sharing options...
proggR Posted March 27, 2009 Author Share Posted March 27, 2009 I get the error even when I know there is checkboxes checked. changed $VAR to $VAR[] and now when it prints I get Array mushrooms. It doesn't echo any of the other checkboxes. Mushrooms was the last checkbox checked. There are empty checkboxes after it though. Link to comment https://forums.phpfreaks.com/topic/151346-foreach-from-_get/#findComment-795155 Share on other sites More sharing options...
Maq Posted March 27, 2009 Share Posted March 27, 2009 Read this. Link to comment https://forums.phpfreaks.com/topic/151346-foreach-from-_get/#findComment-795184 Share on other sites More sharing options...
proggR Posted March 27, 2009 Author Share Posted March 27, 2009 I read through that age and it didn't work either. So i changed the information being sent. The names are now "topping0", "topping1" etc and I have a for loop go through and check if its null and if not it increments the needed value and displays the contents like I need. This may also help me with a javascript event handler I'm working on within the form it gets sent from because now I can individually check and uncheck the boxes I need when certain buttons are pushed. Thanks for everyone's help. It helped me realize what was going on. Link to comment https://forums.phpfreaks.com/topic/151346-foreach-from-_get/#findComment-795204 Share on other sites More sharing options...
shlumph Posted March 27, 2009 Share Posted March 27, 2009 How the heck do you set an array to a single query string variable? Link to comment https://forums.phpfreaks.com/topic/151346-foreach-from-_get/#findComment-795212 Share on other sites More sharing options...
Maq Posted March 27, 2009 Share Posted March 27, 2009 All of your checkbox name are: name="toppings[]" Right? Link to comment https://forums.phpfreaks.com/topic/151346-foreach-from-_get/#findComment-795215 Share on other sites More sharing options...
shlumph Posted March 27, 2009 Share Posted March 27, 2009 Link to comment https://forums.phpfreaks.com/topic/151346-foreach-from-_get/#findComment-795217 Share on other sites More sharing options...
Maq Posted March 27, 2009 Share Posted March 27, 2009 How the heck do you set an array to a single query string variable? I'm not sure what you mean by "array to a single query string variable", or why you're winking at me... winks are scary! Link to comment https://forums.phpfreaks.com/topic/151346-foreach-from-_get/#findComment-795221 Share on other sites More sharing options...
Jagarm Posted March 27, 2009 Share Posted March 27, 2009 if you do $var = $_GET then you'll have an array, but $_GET['toppings'] is not an array. If you are trying to get the checkboxes, first make sure your form is set to method = "post" and now when you do $_POST['topping'] you will get an array Link to comment https://forums.phpfreaks.com/topic/151346-foreach-from-_get/#findComment-795224 Share on other sites More sharing options...
Brian W Posted March 27, 2009 Share Posted March 27, 2009 shlumph, you can use arrays in the query string... it looks like this ?check[]=1&check[]=2&check[]=3 Rather messy if you ask me, I'd use the post method instead... Link to comment https://forums.phpfreaks.com/topic/151346-foreach-from-_get/#findComment-795226 Share on other sites More sharing options...
shlumph Posted March 27, 2009 Share Posted March 27, 2009 I'm not sure what you mean by "array to a single query string variable", or why you're winking at me... winks are scary! LOL - just trying to help the thread OP Link to comment https://forums.phpfreaks.com/topic/151346-foreach-from-_get/#findComment-795419 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.