StormTheGates Posted May 20, 2008 Share Posted May 20, 2008 Ive got a list of checkboxes that I need to loop through. So heres what Ive done so far. <?php echo "$_POST[idofcar]"; foreach ($_POST['idofcar'] as $key => $value) { die("This is working!"); } ?> When this is run the ID of the car from the checkbox will run fine, and say 19 or whatever. But then it will not die which means that it is not going into the loop. Any ideas why? Quote Link to comment https://forums.phpfreaks.com/topic/106489-solved-foreach-problems/ Share on other sites More sharing options...
rhodesa Posted May 20, 2008 Share Posted May 20, 2008 What does your form code look like? Also, what is the output of print_r($_POST['idofcar']); Quote Link to comment https://forums.phpfreaks.com/topic/106489-solved-foreach-problems/#findComment-545847 Share on other sites More sharing options...
roopurt18 Posted May 20, 2008 Share Posted May 20, 2008 I find that for debugging purposes, this line is much more useful: echo '<pre>' . print_r($theVariable, true) . '</pre>'; As for your specific question, if the echo statement prints 19, then that means $_POST['idofcar'] is a string or number, which is a scalar value. foreach works on arrays, not scalars. Quote Link to comment https://forums.phpfreaks.com/topic/106489-solved-foreach-problems/#findComment-545848 Share on other sites More sharing options...
rhodesa Posted May 20, 2008 Share Posted May 20, 2008 Also, just a guess, but your HTML probably looks like: <input type="text" name="idofcar" /> <input type="text" name="idofcar" /> <input type="text" name="idofcar" /> <input type="text" name="idofcar" /> you will need to add [] to them... <input type="text" name="idofcar[]" /> <input type="text" name="idofcar[]" /> <input type="text" name="idofcar[]" /> <input type="text" name="idofcar[]" /> Quote Link to comment https://forums.phpfreaks.com/topic/106489-solved-foreach-problems/#findComment-545853 Share on other sites More sharing options...
StormTheGates Posted May 20, 2008 Author Share Posted May 20, 2008 Yes that worked. Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/106489-solved-foreach-problems/#findComment-545856 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.