MDanz Posted May 15, 2011 Share Posted May 15, 2011 i have a form with multiple checkboxes, with the same name. so i put them in an array like this. <input type='checkbox' name='test[]' value='' /> <input type='checkbox' name='test[]' value='' /> <input type='checkbox' name='test[]' value='' /> <input type='checkbox' name='test[]' value='' /> Now when i do print_r($_POST['test']; , it should display an array and if each checkbox has been ticked or not. it just comes up blank. Any idea? Link to comment https://forums.phpfreaks.com/topic/236434-checkbox-array-help/ Share on other sites More sharing options...
fugix Posted May 15, 2011 Share Posted May 15, 2011 instead of print_r($_POST['test']); try print_r($_POST['test'][]); and if that doesn't work, try to print one result by trying print_r($_POST['test'][0]); Link to comment https://forums.phpfreaks.com/topic/236434-checkbox-array-help/#findComment-1215552 Share on other sites More sharing options...
Zane Posted May 15, 2011 Share Posted May 15, 2011 The only reason I can't think of it not showing anything is that nothing was checked. It would probably help more to print_r the entire $_POST array and see what's coming through. Also, put it inside tags when you print_r, it's much easier to read. [code]echo "" . print_r($_POST) . "";[/code] Link to comment https://forums.phpfreaks.com/topic/236434-checkbox-array-help/#findComment-1215554 Share on other sites More sharing options...
MDanz Posted May 15, 2011 Author Share Posted May 15, 2011 The only reason I can't think of it not showing anything is that nothing was checked. It would probably help more to print_r the entire $_POST array and see what's coming through. Also, put it inside <pre> tags when you print_r, it's much easier to read. echo "<pre>" . print_r($_POST) . "</pre>"; ok i did that and this the result Array ( [0] => ) is it because value is blank? <input type='checkbox' name='test[]' value='' /> Link to comment https://forums.phpfreaks.com/topic/236434-checkbox-array-help/#findComment-1215557 Share on other sites More sharing options...
MDanz Posted May 15, 2011 Author Share Posted May 15, 2011 ok i removed value <input type='checkbox' name='test[]' /> <input type='checkbox' name='test[]' /> and it displays Array ( [0] => on ) but it should be Array ( [0] => off [1] => on ) Link to comment https://forums.phpfreaks.com/topic/236434-checkbox-array-help/#findComment-1215558 Share on other sites More sharing options...
MDanz Posted May 15, 2011 Author Share Posted May 15, 2011 ok i realised what is wrong... if i didn't click on the checkbox it isn't added to the array. <?php if(isset($_POST['submit'])) { print_r($_POST['tags']); } else { ?> <form action="test16.php" method="post"> <input type="checkbox" name="tags[]" value="1" /> <input type="checkbox" name="tags[]" value="2" /> <input type="checkbox" name="tags[]" value="3" /> <input type="checkbox" name="tags[]" value="4" /> <input type="submit" name="submit" value="submit" /> </form> <?php } ?> How do i change the code above so it's four values in the array, indicating what i have clicked on and what i haven't. Link to comment https://forums.phpfreaks.com/topic/236434-checkbox-array-help/#findComment-1215571 Share on other sites More sharing options...
fugix Posted May 15, 2011 Share Posted May 15, 2011 have you tried what i posted? Link to comment https://forums.phpfreaks.com/topic/236434-checkbox-array-help/#findComment-1215573 Share on other sites More sharing options...
MDanz Posted May 15, 2011 Author Share Posted May 15, 2011 yes, i've determined the problem though.. it's unchecked checkboxes aren't added into the array. Link to comment https://forums.phpfreaks.com/topic/236434-checkbox-array-help/#findComment-1215575 Share on other sites More sharing options...
fugix Posted May 15, 2011 Share Posted May 15, 2011 that's correct, when you tested this did you not have any checkboxes checked? Link to comment https://forums.phpfreaks.com/topic/236434-checkbox-array-help/#findComment-1215577 Share on other sites More sharing options...
MDanz Posted May 15, 2011 Author Share Posted May 15, 2011 nvm i found a solution .. thx anyway Link to comment https://forums.phpfreaks.com/topic/236434-checkbox-array-help/#findComment-1215579 Share on other sites More sharing options...
anupamsaha Posted May 15, 2011 Share Posted May 15, 2011 Please mark the topic as "solved" if you have a solution or satisfied with the answer provided. Thanks! Link to comment https://forums.phpfreaks.com/topic/236434-checkbox-array-help/#findComment-1215596 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.